如何获取所有xml节点和值

本文关键字:xml 节点 何获取 获取 | 更新日期: 2023-09-27 18:19:45

我有一个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节点和值

以下是您要查找的代码

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

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