【问题标题】:How get the attribute value of XML node depending on another attribute value in C#如何根据C#中的另一个属性值获取XML节点的属性值
【发布时间】:2021-02-23 07:36:57
【问题描述】:

这是 XML:

     <MatML_Doc>
        <Material>
           <BulkDetails>
               <Name>ABS</Name>
               <Class> <Name>PLASTIC</Name></Class>
               <Subclass> <Name>ABS Polymer</Name></Subclass>
               <PropertyData property="Material_Type">
                   <Data format="string">IsotropicMaterial</Data>
               </PropertyData>
               <PropertyData property="Version">
                   <Data format="string">4.0</Data>
               </PropertyData>
               <PropertyData property="Category">
                   <Data format="string">PLASTIC</Data>
               </PropertyData>      
               <PropertyData property="CoatingsStudioMaterialName">
                 <Data format="string">ABS Plastic</Data>
               </PropertyData>
               <PropertyData property="CoatingsVisualizationColor">
                 <Data format="exponential">168, 168, 168</Data>
               </PropertyData>
               <PropertyData property="ColorID">
                 <Data format="integer">87</Data>
               </PropertyData>
           </BulkDetails>
        </Material>
   <MatML_Doc>
   

如果节点“Name”的值为 ABS,我想获取数据属性“ColorID”的值(我想要数字 87)。 通常的程序是什么?

我在这里添加我的解决方案:

private static int GetColorIdInXmlByMaterial(string material, XmlDocument doc)
 {
    XmlElement element = doc.DocumentElement;
    XmlNodeList xmlNodeList = element.SelectNodes("//BulkDetails");

    foreach(XmlNode node in xmlNodeList)
    {
        if (node["Name"].InnerText.Equals(material))
        {
            return Convert.ToInt32(node["ColorID"].InnerText);
        }
    }
    return 0;
 }

【问题讨论】:

  • 先获取名称,然后检查是否为“ABS”,即可获取颜色ID。

标签: c# .net xml dom


【解决方案1】:

使用 xml linq:

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 FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Material> meterials = doc.Descendants("Material").Select(x => new Material()
            {
                name = (string)x.Descendants("Name").FirstOrDefault(),
                className = (string)x.Descendants("Class").FirstOrDefault().Element("Name"),
                properties = x.Descendants("PropertyData").Select(y => new Property()
                {
                    property = (string)y.Attribute("property"),
                    format = (string)y.Element("Data").Attribute("format"),
                    value = (string)y.Element("Data")
                }).ToList()
            }).ToList();

            Dictionary<string, Material> dict = materials.GroupBy(x => x.name, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            Material lookup = dict["ABS"];
        }
    }
    public class Material
    {
        public string name { get; set; }
        public string className { get; set; }
        public List<Property> properties { get; set; }
    }
    public class Property
    {
        public string property { get; set; }
        public string format { get; set; }
        public string value { get; set; }
    }
}

【讨论】:

  • 感谢您的建议。我的 xml 文件有超过 10k 行。我读过 xml linq 在处理大数据时性能很差。那是怎么回事? :)
  • 没有。 Xml 序列化性能较差。 Linq 要快得多。我在代码中添加了一个字典。 Linq 在某些条件下从数据库读取时可能会很慢,如果超出机器的内存,也会很慢。
  • 与我的解决方案相比有优势吗?因为我发现理解 linq 解决方案要困难得多
  • XmlDocument 是 Net Xml 库的旧版本,XDocument 是 Net Library 的新版本。 XmlDocument 要复杂得多,需要更多的代码行。我发现 XmlDocument 比 XDocument 更难使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多