繁体   English   中英

如何获取所有xml节点和值

[英]How to get all xml nodes and values

我有一个xml,想从中获取所有节点和值。 下面是源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

我要创建的输出包括2列,节点及其值。 我该如何实现? 我应该使用XmlDocument类吗?

我建议使用一种递归方法,该方法接受一个包含当前路径和XmlNode的字符串。 在每个递归中,您似乎想遍历所有节点属性,然后遍历每个子节点。 检查您的NodeType以确定何时到达递归末尾。

将原始XML加载到XmlDocument中,然后从DocumentElement节点开始调用递归方法。

编辑:

不是我做过的最漂亮的代码,但它接受给定的输入并产生请求的输出:

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

这是您要查找的代码

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