简体   繁体   English

解析全局节点内的多个xml节点和子节点?

[英]Parse multiple xml nodes and child-nodes inside global node?

How i can parse multiple nodes & child nodes in one XML file? 如何在一个XML文件中解析多个节点和子节点? Here the file structure: 这里的文件结构:

<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>

I am try to make a reader-parser for this, and him sucessufy reads object and attribute keys, but i dont have idea, how i can read node and their children nodes in this format (as on xml example) My reader is: 我试图为此做一个读者解析器,他成功读取了对象和属性键,但是我不知道如何读取这种格式的节点及其子节点(如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;
                    }
                }
            }
        }

Please, any solution in C# for XML parse nodes and child nodes in this case? 请在这种情况下,使用C#中的XML解析节点和子节点的任何解决方案? I think nodeList=root.SelectNodes() - non better idea in this cause. 我认为nodeList=root.SelectNodes() -在这个原因中没有更好的主意。 <subobjects> - is children node of object, and this have a their non-static (for each object - different) type of sub-object. <subobjects> -是<subobjects>节点,并且具有其子对象的非静态(对于每个对象-不同)类型。 Any idea? 任何想法?

Would a recursive function work? 递归函数可以工作吗?

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);
            }
        }
    }

You could use XmlNode.Name to know which node you're dealing with. 您可以使用XmlNode.Name知道您正在处理哪个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM