【问题标题】:How to get all xml nodes and values如何获取所有xml节点和值
【发布时间】:2015-10-29 17:06:43
【问题描述】:

我有一个 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 类吗?

【问题讨论】:

  • 我目前没有看到任何 XML...当您 确实 有一些 XML 时,我建议一般使用 LINQ to XML。您可以使用Descendants() 获取所有元素,然后对于每个元素,您可以使用AncestorsAndSelf() 构造一个名称链。我建议您尝试使用这么多信息,并包括您在问题中的进展程度(以及 XML)。
  • 源 XML 肯定是有益的,因为从输出来看,它看起来不是有效的 XML。我不认为 节点允许同时具有 <lang> 子节点和文本值(日常意大利语)。</lang>
  • 刚刚发布了您需要的代码。如果对您有用,请标记为答案。它产生您需要的完全相同的输出。谢谢。

标签: c# .net xml-parsing


【解决方案1】:

这是您要查找的代码

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

}

【讨论】:

    【解决方案2】:

    我会建议一种递归方法,它接受包含当前路径和要进入的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多