c# - 使用 C# 解析 XML 字符串

标签 c# xml

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

<items>
    <item1 value="value1" />
    <item2 value="value2" />
    <item3 value="value3" />
    <itme4 value="value4" />
    <item5 value="value5" />
    <itme6 value="value6" />
</items>

我正在尝试按照以下方式进行操作:

    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 = @"<items>
<item1 value=""value1"" />
<item2 value=""value2"" />
<item3 value=""value3"" />
<itme4 value=""value4"" />
<item5 value=""value5"" />
<item6 value=""value6"" />
</items>";

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。

关于c# - 使用 C# 解析 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031852/

相关文章:

c# - 在 Lucene.Net 索引中搜索 url 字段

html - 如何解析 HTML 以搜索特定内容的针

java - 显示启动画面,然后显示 AppIntro(首次启动)而不是 mainactivity 不工作

java - Jaxb 可以编码没有根元素的子元素吗?

xml - 使用网络服务?

c# - ReactiveCommand 的前后 Action

c# - ConfigurationManager.ConnectionStrings.Add

c# - 以可读格式发回文件?

c# - 检查数据库中的值以查看它是否是 bool 值并设置组合框,这样会引发异常

c# - xml错误代码文件