使用C#解析XML字符串

我需要使用C#解析一个简单的XML字符串

        

我正在努力做到如下:

  XMLParser XMLParserObj = new XMLParser(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlString); string item1 = xmlDoc.Attributes.GetNamedItem("item1").Value; string item2 = xmlDoc.Attributes.GetNamedItem("item2").Value; string item3 = xmlDoc.Attributes.GetNamedItem("item3").Value; string item4 = xmlDoc.Attributes.GetNamedItem("item4").Value; string item5 = xmlDoc.Attributes.GetNamedItem("item5").Value; string item6 = xmlDoc.Attributes.GetNamedItem("item6").Value; 

没有运气

请指教..

一个可能的解决方案是使用LINQ2XML :

 // retrieve a single element by tag name private XElement getElement(XDocument xmlDocument, string elementName) { var element = xmlDocument.Descendants("items").Elements().Where (x => x.Name == elementName).FirstOrDefault(); return element != null ? element : null; } // retrieve attribute by name private string attributeValue(XElement item, string attributeName) { var attribute = item.Attribute(attributeName); return attribute != null ? attribute.Value : string.Empty; } 

这两个函数可以像这样使用:

 // input for the example var input = @"       "; var xml = XDocument.Parse(input); var element = getElement(xml, "item2"); if (element != null) { Console.WriteLine(attributeValue(element, "value")); } 

输出是:

 value2 

对于像这样的重复性任务,将方法实现为扩展通常是有利的:

 public static class ProjectExtensions { // retrieve a single element by tag name public static XElement GetElementByName(this XDocument xmlDocument, string parentElementName, string elementName) { var element = xmlDocument.Descendants(parentElementName).Elements().Where (x => x.Name == elementName).FirstOrDefault(); return element != null ? element : null; } // retrieve attribute by name public static string GetAttributeValueByName(this XElement item, string attributeName) { var attribute = item.Attribute(attributeName); return attribute != null ? attribute.Value : string.Empty; } } 

IMO的使用更加简单和清洁:

 var xml = XDocument.Parse(input); var element = xml.GetElementByName("items", "item2"); if (element != null) { Console.WriteLine(element.GetAttributeValueByName("value")); } 

您当然可以(如已经建议的那样)直接使用XPath。

保留所有代码相同但更改此内容:

 string item1 = xmlDoc.GetElementsByTagName("item1")[0].Attributes[0].Value; 

为其他项目做同样的事情,你就会得到价值。

假设您想要每个item元素的value属性,您可以使用Xpath:

  string xmlString = ""; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlString); string item1 = xmlDoc.SelectSingleNode("items/item1/@value").InnerText; string item2 = xmlDoc.SelectSingleNode("items/item2/@value").InnerText; string item3 = xmlDoc.SelectSingleNode("items/item3/@value").InnerText; string item4 = xmlDoc.SelectSingleNode("items/item4/@value").InnerText; string item5 = xmlDoc.SelectSingleNode("items/item5/@value").InnerText; string item6 = xmlDoc.SelectSingleNode("items/item6/@value").InnerText; 

你错过了“属性”一词,你试图捕捉“item1”,它实际上是一个节点。

用这个:

 string item1 = xmlDoc.SelectSingleNode("//items/item1").Attributes["value"].value; 

还有其他选项,但这会为您提供所需的值。

干杯…

这将获得所有这些属性值:

1)如果来自字符串XDocument.Parse(),则使用XDocument.Load()加载xml,然后执行以下操作:

 IEnumerable result = from node in xmlDoc.Root.Descendants() select node.Attribute("value").Value;