如何获取所有xml节点和值

时间:2015-10-29 17:06:44

标签: c# .net xml-parsing

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

以下是您要查找的代码

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

}