简体   繁体   English

如何获取所有xml节点和值

[英]How to get all xml nodes and values

I have an xml and want to get all the nodes and values from it. 我有一个xml,想从中获取所有节点和值。 Below an example of the source xml and the output I want to create: 下面是源xml和我要创建的输出的示例:

<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price curr="$">30.00</price>
  </book>
</bookstore>

Nodes   Values
bookstore.book.category COOKING
bookstore.book.title.lang   en
bookstore.book.title    Everyday Italian
bookstore.book.author   Giada De Laurentiis
bookstore.book.year 2005
bookstore.book.price.curr   $
bookstore.book.price    30

The output I want to create consists of 2 columns, nodes and it's value. 我要创建的输出包括2列,节点及其值。 How can I achieve this? 我该如何实现? Should I use XmlDocument class? 我应该使用XmlDocument类吗?

I would suggest a recursive method that accepts a string containing the current path and the XmlNode to go into. 我建议使用一种递归方法,该方法接受一个包含当前路径和XmlNode的字符串。 In each recursion it looks like you want to loop through all the node attributes and then each child node. 在每个递归中,您似乎想遍历所有节点属性,然后遍历每个子节点。 Check your NodeType to determine when you have reach the end of the recursion. 检查您的NodeType以确定何时到达递归末尾。

Load the original XML into an XmlDocument, then call your recursive method starting with the DocumentElement node. 将原始XML加载到XmlDocument中,然后从DocumentElement节点开始调用递归方法。

EDIT: 编辑:

Not the prettiest code I've ever done but it takes the given input and produces the requested output: 不是我做过的最漂亮的代码,但它接受给定的输入并产生请求的输出:

static void Main(string[] args)
{
    string xmlSrc = @"<bookstore><book category=""COOKING""><title lang=""en"">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price curr=""$"">30.00</price></book></bookstore>";

    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(xmlSrc);

    StringBuilder sbOut = new StringBuilder();
    sbOut.AppendLine("Nodes\tValues");
    sbOut.Append(XmlToText(xDoc.DocumentElement));

    Console.WriteLine(sbOut.ToString());
    Console.WriteLine("Press any key to exit...");
    Console.ReadLine();
}


static StringBuilder XmlToText(XmlElement node, string generationPath = "")
{
    StringBuilder sbRet = new StringBuilder();

    generationPath += (String.IsNullOrEmpty(generationPath) ? "" : ".") + node.Name;

    foreach( XmlAttribute attr in node.Attributes)
    {
        sbRet.AppendLine(String.Format("{0}.{1}\t{2}", generationPath, attr.Name, attr.Value));
    }

    foreach( XmlNode child in node.ChildNodes)
    {
        if( child.NodeType == XmlNodeType.Element)
        {
            sbRet.Append(XmlToText(child as XmlElement, generationPath));
        }
        else if ( child.NodeType == XmlNodeType.Text)
        {
            sbRet.AppendLine(String.Format("{0}\t{1}", generationPath, child.InnerText));
        }
    }

    return sbRet;
}

Here is the code you are looking for 这是您要查找的代码

private static void PrintOutNodesRecursive(XmlElement xmlElement, string currentStack)
{
    foreach (XmlAttribute xmlAttribute in xmlElement.Attributes)
    {
        Console.WriteLine("{0}.{1} = {2}", currentStack, xmlAttribute.Name, xmlAttribute.Value);
    }
    foreach (XmlNode xmlNode in xmlElement.ChildNodes)
    {
        XmlElement xmlChildElement = xmlNode as XmlElement;
        XmlText xmlText = xmlNode as XmlText;

        if (xmlText != null)
        {
            Console.WriteLine("{0} = {1}", currentStack, xmlText.Value);
        }

        if (xmlChildElement != null)
        {
            PrintOutNodesRecursive(xmlChildElement, currentStack + "." + xmlChildElement.Name);
        }
    }
}


static void Main(string[] args)
{
    string xmlContent = @"<bookstore>
      <book category=""COOKING"">
        <title lang=""en"">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price curr=""$"">30.00</price>
      </book>
    </bookstore>";

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(xmlContent);

    PrintOutNodesRecursive(xmlDocument.DocumentElement, xmlDocument.DocumentElement.Name);

}

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

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