解析多个XML节点和子节点的全球节点内? [英] Parse multiple xml nodes and child-nodes inside global node?

查看:211
本文介绍了解析多个XML节点和子节点的全球节点内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以解析多个节点和放大器;在一个XML文件中的子节点?
下面的文件结构:



<预类=郎咸平的XML prettyprint-覆盖> <对象和GT;

<对象ID = 1 name =测试参数=1>
<属性键=测试值=1/>

<&子对象GT;
<子对象ID = 2的值=测试参数=一些/>
< subobject2 ID = 2的值=测试参数=一些/>
< subobject3 ID = 2的值=测试参数=一些/>
< /子对象>

< /对象>

<对象ID = 2名=newtest参数=44>
<属性键=测试1值=1/>
<属性键=test2的价值=2/>
<属性键=TEST3VALUE =3/>

<&子对象GT;
<子对象ID = 1值=intvaluetest参数=1/>
< /子对象>

< /对象>

< /对象>



我试图让读者解析器对于这一点,和他sucessufy读取对象和属性键,
,但我没有想法,我怎么能以这种格式读取节点及其子节点(如XML的例子)
我的读者是:



<预类=郎-CS prettyprint-覆盖> 的XmlDocument文档=新的XmlDocument();
document.Load(文件名);

的foreach(在document.GetElementsByTagName XmlNode的节点(对象))​​
{
USHORT ID = ushort.Parse(node.Attributes [ID]的InnerText。);
//做一些事情

的foreach(在node.Attributes XmlAttribute attr)使用
{
开关(attr.Name.ToLower())
{
案名:
//做事
中断;
案参数:
//做事
中断;
}
}

如果(node.HasChildNodes)
{
的foreach(XmlNode的attrNode在node.ChildNodes)
{
如果(attrNode.Attributes =空&放大器;!&放大器;
attrNode.Attributes.Count大于0&放大器;&放大器;
attrNode.Attributes [键] =空&放大器;!&放大器;
attrNode.Attributes [值] = NULL)
{
字符串值= attrNode.Attributes [值]的InnerText!。
开关(attrNode.Attributes [钥匙] InnerText.ToLower()。)
{
案考:
//做的事情与价值
断点;
}
}
}
}

请,在这种情况下,XML解析节点和子节点在C#任何溶液?我觉得节点列表= root.SelectNodes() - 这项事业非更好的主意。 <&子对象GT; - 为对象的子节点,这有其非静态的(每个对象 - 不同)类型的子对象。任何想法?


解决方案

请问递归函数的工作?



 静态无效的主要(){
XmlDocument的DOC =新的XmlDocument();
doc.Load(的test.xml);

ReadNode(doc.GetElementsByTagName(对象)[0]);
到Console.ReadLine();
}

静态无效ReadNode(XmlNode的节点){
//完成与每个节点
的东西,如果(node.Attributes.Count大于0){
的foreach(在node.Attributes XmlAttribute ATTR){
//使用每个属性
}东西如果
}
(node.HasChildNodes ==真){
的foreach(在node.ChildNodes XmlNode的chlnode){
ReadNode(chlnode);
}
}
}

您可以使用 XmlNode.Name 来知道你处理的节点。


How i can parse multiple nodes & child nodes in one XML file? 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:

    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? I think nodeList=root.SelectNodes() - non better idea in this cause. <subobjects> - is children node of object, and this have a their non-static (for each object - different) type of sub-object. 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.

这篇关于解析多个XML节点和子节点的全球节点内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆