0

我必须解析 XML,因为它使用了一些有效的代码,但问题不是我们嵌套了 XML,这个代码无法解析所有数据。所以请建议我如何分割它。

XML

<Author>
  <Book id="101" name="Computer" subcategories="2">
    <Book id="600" name="java" subcategories="0" contents="10 books"/>
    <Book id="601" name="php" subcategories="0" contents="5 books"/>
  </Book>
  <Book id="201" name="Language" subcategories="2">
    <Book id="700" name="Hindi" subcategories="0" contents="6 books"/>
    <Book id="701" name="English" subcategories="0" contents="4 books"/>
  </Book>
  <Book id="301" name="Music" subcategories="2">
    <Book id="800" name="life" subcategories="0" contents="10 books"/>
    <Book id="801" name="Wild" subcategories="0" contents="5 books"/>
  </Book>
  <Book id="401" name="Story" subcategories="2">
    <Book id="900" name="My Life" subcategories="0" contents="1 books"/>
    <Book id="901" name="One Day" subcategories="0" contents="1 books"/>
  </Book>
</Author>

解析器方法:

public void parser()
        {
            XElement nodes = XElement.Load("file.xml");

            var node = from nd in nodes.DescendantNodes() select nd;

            foreach (XElement nds in node)
            {
                string name = nds.Attribute("name").Value;
                int id=Convert.ToInt32(nds.Attribute("id").Value);
                MessageBox.Show("" + name);
                MessageBox.Show("" + id);
                if (nds.HasElements)
                {
                     getChild(nds.DescendantNodes(),id);
                }

            }
 public void getChild(IEnumerable<XNode> node,int id)
        {

            foreach (XElement nds in node)
            {


                IEnumerable<XNode> nd=from list in nds.DescendantNodes() select list;
                int temp = Convert.ToInt32(ids);



                    foreach (XElement ss in nd)
                    {
                        string name = ss.Attribute("name").Value;
                        MessageBox.Show("" + name);
                    }

                if (nds.HasElements)
                {
                    getChild(nds.DescendantNodes(),id);
                }
            }
        }

我有上面的 XML 并使用上面的代码进行解析。

4

1 回答 1

1

也许你正在寻找这个:

XElement doc = XElement.Parse(@"<Author>
                                  <Book id=""101"" name=""Computer"" subcategories=""2"">
                                    <Book id=""600"" name=""java"" subcategories=""0"" contents=""10 books""/>
                                    <Book id=""601"" name=""php"" subcategories=""0"" contents=""5 books""/>
                                  </Book>
                                  <Book id=""201"" name=""Language"" subcategories=""2"">
                                    <Book id=""700"" name=""Hindi"" subcategories=""0"" contents=""6 books""/>
                                    <Book id=""701"" name=""English"" subcategories=""0"" contents=""4 books""/>
                                  </Book>
                                  <Book id=""301"" name=""Music"" subcategories=""2"">
                                    <Book id=""800"" name=""life"" subcategories=""0"" contents=""10 books""/>
                                    <Book id=""801"" name=""Wild"" subcategories=""0"" contents=""5 books""/>
                                  </Book>
                                  <Book id=""401"" name=""Story"" subcategories=""2"">
                                    <Book id=""900"" name=""My Life"" subcategories=""0"" contents=""1 books""/>
                                    <Book id=""901"" name=""One Day"" subcategories=""0"" contents=""1 books""/>
                                  </Book>
                                </Author>");
var query = doc.Descendants();
foreach(XElement ele in query){
    Console.WriteLine(string.Format("Name: {0}\nValue: {1}\n",ele.Name.LocalName.ToString(),ele));
    if (ele.HasAttributes){
        var query1 = ele.Attributes();
        foreach (XAttribute att in query1)
        {
            Console.WriteLine(string.Format("Attribute: {0}\nValue: {1}\n", att.Name.LocalName.ToString(), att.Value));
        };
    };
};
于 2013-02-01T11:07:03.870 回答