【问题标题】:How can I get text and attributes values in my XML如何在我的 XML 中获取文本和属性值
【发布时间】:2015-12-22 18:25:07
【问题描述】:

XML 示例:

<?xml version="1.0" encoding="utf-8" ?>
<brand name="brand1" num_brand="118" enabled="True">
  <price>
    <nodePattern>pattern</nodePattern>
    <attribute type="text" ></attribute>
    <treatment enabled="1" type="Regex">reg</treatment>
  </price>
  <title>
    <nodePattern>pattern</nodePattern>
    <attribute type="text" ></attribute>
    <treatment enabled="1" type="Regex">reg</treatment>
  </title>
</brand>

请问,如何使用 System.Xml.Linq 获取所有不同节点的不同属性值和文本(例如 name、num_brand 和 enabled for brand、enabled、type 和 "reg" for treatment)?

谢谢!

【问题讨论】:

  • 到目前为止你尝试过什么?你在挣扎什么?您想如何获取这些值(在单个字符串中?加载到类中?)
  • 现在的问题是你的问题是什么?根据您的需要,Linq to XML 非常简单。例如var x = doc.Root.Attribute("num_brand").Value; 为您提供该属性的文本..

标签: c# .net xml linq


【解决方案1】:

System.Xml.Linq 命名空间比 System.Xml 命名空间好得多。您的XDocument 有一个XElement,而XElement 又具有子元素。每个元素都有属性和值。

这里有一个例子:

var text = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <brand name=""brand1"" num_brand=""118"" enabled=""True"">
                <price>
                <nodePattern>pattern</nodePattern>
                <attribute type=""text"" ></attribute>
                <treatment enabled=""1"" type=""Regex"">reg</treatment>
                </price>
                <title>
                <nodePattern>pattern</nodePattern>
                <attribute type=""text"" ></attribute>
                <treatment enabled=""1"" type=""Regex"">reg</treatment>
                </title>
            </brand>";

XDocument document = XDocument.Parse(text);

// one root element - "brand"          
System.Diagnostics.Debug.Assert(document.Elements().Count() == 1);
XElement brand = document.Element("brand");

// brand has two children - price and title           
foreach (var element in brand.Elements())
    Console.WriteLine("element name: " + element.Name);

// brand has three attributes
foreach (var attr in brand.Attributes())
    Console.WriteLine("attribute name: " + attr.Name + ", value: " + attr.Value);

【讨论】:

    【解决方案2】:

    你有很多方法可以做到这一点。其中之一是 XmlDocument。

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(myXML);
    foreach(XmlNode node in xmlDoc.DocumentElement.ChildNodes){
        string text = node.InnerText; //you can loop through children
    }
    

    看看这篇文章: How do I read and parse an XML file in C#?

    我个人喜欢 Linq To Xml 方法,更多信息在这里: https://msdn.microsoft.com/en-us/library/bb387061.aspx

    【讨论】:

    • 您的回答建议使用XmlDocument,而不是使用System.Xml.Linq(问题是关于什么的)。它还指定如何获取 InnerText 而不是属性值...
    【解决方案3】:

    试试这个

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENMAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENMAME);
    
                var brand = doc.Descendants("brand").Select(x => new
                {
                    name = x.Attribute("name").Value,
                    num_brand = x.Attribute("num_brand").Value,
                    enabled = x.Attribute("enabled").Value,
                    nodePattern = x.Element("price").Element("nodePattern").Value,
                    attribute = x.Element("price").Element("attribute").Attribute("type").Value,
                    priceTreatmentEnable = x.Element("price").Element("treatment").Attribute("enabled").Value,
                    priceTreatmentType = x.Element("price").Element("treatment").Attribute("type").Value,
                    priceTreatment = x.Element("price").Element("treatment").Value,
                    titleTreatmentEnable = x.Element("title").Element("treatment").Attribute("enabled").Value,
                    titleTreatmentType = x.Element("title").Element("treatment").Attribute("type").Value,
                    titleTreatment = x.Element("title").Element("treatment").Value
                }).FirstOrDefault();
            }
        }
    }
    ​
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2021-08-07
      相关资源
      最近更新 更多