1. 程式人生 > >C# 讀取 XML文件

C# 讀取 XML文件

doc des img 分享 child add 類型 blog pri

技術分享

public static List<XmlData> GetXmlData( string path,string name)
{
List<XmlData> XmlList = new List<XmlData>();
XmlDocument documwent = new XmlDocument();
documwent.Load(path);
XmlNode rootNode = documwent.SelectSingleNode("metalib");
XmlNodeList childNode = rootNode.ChildNodes;
foreach (XmlNode dataNode in childNode)
{
XmlElement xe = (XmlElement)dataNode;
if (xe.GetAttribute("name").ToString() == name)
{
XmlNodeList infoNode = xe.ChildNodes;
foreach(XmlNode data in infoNode)
{
XmlData xmlData = new XmlData();
if (xmlData!=null)
{
XmlElement attribute = (XmlElement)data;
xmlData.Name = attribute.GetAttribute("name").ToString();
xmlData.Type = attribute.GetAttribute("type").ToString();
xmlData.CName = attribute.GetAttribute("cname").ToString();
XmlList.Add(xmlData);
}

}
return XmlList;
}

}
return null;
}
}
public class XmlData
{
/// <summary>
/// 屬性名
/// </summary>
private string m_Name = null;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
/// <summary>
/// 屬性類型
/// </summary>
private string m_Type = null;
public string Type
{
get { return m_Type; }
set { m_Type = value; }
}
/// <summary>
///
/// </summary>
private string m_CName = null;
public string CName
{
get { return m_CName; }
set { m_CName = value; }

}

}

C# 讀取 XML文件