如何获得XML元素值使用的XDocument字符串
展开全部
public static List<string> GetXmlList(string xmlPath, string tagName,string element1,string element2)
{
string strPath = xmlPath;
if (!File.Exists(strPath))
{
return null;
}
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(strPath);
//子节点
XmlElement pElements = XmlDoc.DocumentElement;
XmlNodeList pNodeList = pElements.GetElementsByTagName(tagName);
XmlNode dnode = pNodeList[1];
List<string> key = new List<string>();
List<string> value = new List<string>();
List<List<string>> list = new List<List<string>>();
foreach (XmlNode pNode in pNodeList)
{
if (pNode.NodeType.ToString() != "Comment")
{
XmlElement pElement = pNode as XmlElement;
key.Add(pElement.GetAttribute(element1).ToString());
value.Add(pElement.GetAttribute(element2).ToString());
}
}
list.Add(key);
list.Add(value);
return key;
}
{
string strPath = xmlPath;
if (!File.Exists(strPath))
{
return null;
}
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(strPath);
//子节点
XmlElement pElements = XmlDoc.DocumentElement;
XmlNodeList pNodeList = pElements.GetElementsByTagName(tagName);
XmlNode dnode = pNodeList[1];
List<string> key = new List<string>();
List<string> value = new List<string>();
List<List<string>> list = new List<List<string>>();
foreach (XmlNode pNode in pNodeList)
{
if (pNode.NodeType.ToString() != "Comment")
{
XmlElement pElement = pNode as XmlElement;
key.Add(pElement.GetAttribute(element1).ToString());
value.Add(pElement.GetAttribute(element2).ToString());
}
}
list.Add(key);
list.Add(value);
return key;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询