【问题标题】:Parse multiple xml nodes and child-nodes inside global node?解析全局节点内的多个 xml 节点和子节点?
【发布时间】:2023-04-06 08:58:01
【问题描述】:

如何在一个 XML 文件中解析多个节点和子节点? 这里是文件结构:

<objects>

 <object id=1 name="test" param="1">
   <attribute key = "test" value="1"/>

   <subobjects>
    <subobject id=2 value="test" param="some"/>
    <subobject2 id=2 value="test" param="some"/>
    <subobject3 id=2 value="test" param="some"/>
   </subobjects>

 </object>

 <object id=2 name="newtest" param="44">
   <attribute key = "test1" value="1"/>
   <attribute key = "test2" value="2"/>
   <attribute key = "test3" value="3"/>

   <subobjects>
    <subobject id=1 value="intvaluetest" param="1"/>
   </subobjects>

 </object>

</objects>

我正在尝试为此制作一个阅读器解析器,并且他成功读取了对象和属性键, 但我不知道如何以这种格式读取节点及其子节点(如 xml 示例) 我的读者是:

    XmlDocument document = new XmlDocument();
    document.Load(fileName);

    foreach (XmlNode node in document.GetElementsByTagName("object"))
    {
        ushort id = ushort.Parse(node.Attributes["id"].InnerText);
        //do some things

        foreach (XmlAttribute attr in node.Attributes)
        {
            switch (attr.Name.ToLower())
            {
                case "name":
                    //do things
                    break;
                case "param":
                    //do things
                    break;
            }
        }

        if (node.HasChildNodes)
        {
            foreach (XmlNode attrNode in node.ChildNodes)
            {
                if (attrNode.Attributes != null &&
                    attrNode.Attributes.Count > 0 &&
                    attrNode.Attributes["key"] != null &&
                    attrNode.Attributes["value"] != null)
                {
                    string value = attrNode.Attributes["value"].InnerText;
                    switch (attrNode.Attributes["key"].InnerText.ToLower())
                    {
                        case "test":
                            //do things with value
                            break;
                    }
                }
            }
        }

请问,在这种情况下,C# 中是否有针对 XML 解析节点和子节点的解决方案?我认为nodeList=root.SelectNodes() - 在这个原因中没有更好的主意。 &lt;subobjects&gt; - 是对象的子节点,它有一个非静态(对于每个对象 - 不同)类型的子对象。有什么想法吗?

【问题讨论】:

  • 有什么理由不使用XDocument 而不是 XmlDocument(在我给出使用 XDocument 的完整答案之前)?
  • XmlDocument 没有具体原因。但我很高兴看到,如何在 XDocument 上执行此操作!学习 - 永远不会太晚。
  • 您知道您可以使用 XPath 来选择节点...对吗? XmlNodeList MyNodes = XDoc.SelectNodes("//subobject2 | //subobject3");
  • 你想要什么输出?您已经展示了收集值的尝试,但除了输出之外没有显示。
  • 我将读取这些数据,并将每个对象及其子对象添加到对象列表中。每个对象都有 id 和其他参数,类中字典的一些属性和子对象。

标签: c# xml


【解决方案1】:

递归函数会起作用吗?

static void Main() {
    XmlDocument doc = new XmlDocument();
    doc.Load("test.xml");

    ReadNode(doc.GetElementsByTagName("object")[0]);
    Console.ReadLine();
}

static void ReadNode(XmlNode node) {
        //Do something with each Node
        if(node.Attributes.Count > 0) {
            foreach(XmlAttribute attr in node.Attributes) {
                //Do Something with each Attribute
            }
        }
        if(node.HasChildNodes == true) {
            foreach(XmlNode chlnode in node.ChildNodes) {
                ReadNode(chlnode);
            }
        }
    }

您可以使用XmlNode.Name 了解您正在处理的节点。

【讨论】:

  • 是的,这就是使用递归模式的想法。我会试试这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
相关资源
最近更新 更多