如何从xml获取特定节点的所有值?

时间:2016-03-18 15:55:53

标签: c# xml

我有如下的input.xml文件:

<Root Details="false">
  <Product count="45" Name="Success">
      <Project Id="420" Title="First"/>
      <Main Id="220" Title="Last"/>
      <Status>
        <Maintainance Id="123" Title="Good">
            <Maintain Id="ABC" />
            <Maintain Id="DEF" />
            <Maintain Id="GHI" />
        </Maintainance>
        <Maintainance Id="456" Title="Better">
            <Maintain Id="JKL" />
            <Maintain Id="MNO" />
            <Maintain Id="PQR" />
        </Maintainance>
        <Maintainance Id="789" Title="Bad">
            <Maintain Id="STU" />
            <Maintain Id="VWX" />
            <Maintain Id="XYZ" />
        </Maintainance>
       </Status>
      <Main Id="420" Title="Failure"/>
      <Project Id="220" Title="First"/> 
  </Product>
</Root>

我只需要功能节点值,输出xml如下所示: 即我需要输出如output.xml

中的输出
<Root Details="false">
  <Product count="45" Name="Success">
    <Maintainance Id="123" Title="Good">
            <Maintain Id="ABC" />
            <Maintain Id="DEF" />
            <Maintain Id="GHI" />
        </Maintainance>
        <Maintainance Id="456" Title="Better">
            <Maintain Id="JKL" />
            <Maintain Id="MNO" />
            <Maintain Id="PQR" />
        </Maintainance>
        <Maintainance Id="789" Title="Bad">
            <Maintain Id="STU" />
            <Maintain Id="VWX" />
            <Maintain Id="XYZ" />
        </Maintainance>
  </Product>
</Root>

分享任何想法。提前致谢。我正在寻找使用xmldocument的输出。告诉我使用xmldocument获取输出。

2 个答案:

答案 0 :(得分:0)

使用指定的XmlDocument快速而脏的解决方案。可能是一种更好的方法,但是你去......

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;

namespace XmlDocumentTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"C:\java\xml\root.xml");
                var root = doc.DocumentElement;
                var productNode = root.FirstChild;
                var elementsToRemove = new List<XmlElement>();
                foreach (XmlElement element in root.GetElementsByTagName("Project"))
                {
                    elementsToRemove.Add(element);
                }

                foreach (XmlElement element in root.GetElementsByTagName("Main"))
                {
                    elementsToRemove.Add(element);
                }

                var maintList = new List<XmlElement>();
                foreach (XmlElement element in root.GetElementsByTagName("Maintainance"))
                {
                    maintList.Add(element);
                }

                foreach (XmlElement element in maintList)
                {
                    var newNode = element.CloneNode(true);
                    element.ParentNode.ParentNode.AppendChild(newNode);
                }

                foreach (XmlElement element in root.GetElementsByTagName("Status"))
                {
                    elementsToRemove.Add(element);
                }

                foreach (XmlElement element in elementsToRemove)
                {
                    element.ParentNode.RemoveChild(element);
                }

                Console.WriteLine("Xml: " + doc.OuterXml);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            while (Console.ReadKey(true).Key != ConsoleKey.Escape)
            {
            }
        }
    }
}

P.S。维护应该拼写维护:)

答案 1 :(得分:0)

试试这个。与昨天的答案类似。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Root Details=\"false\">" +
                  "<Product count=\"45\" Name=\"Success\">" +
                      "<Project Id=\"420\" Title=\"First\"/>" +
                      "<Main Id=\"220\" Title=\"Last\"/>" +
                      "<Status>" +
                        "<Maintainance Id=\"123\" Title=\"Good\">" +
                            "<Maintain Id=\"ABC\" />" +
                            "<Maintain Id=\"DEF\" />" +
                            "<Maintain Id=\"GHI\" />" +
                        "</Maintainance>" +
                        "<Maintainance Id=\"456\" Title=\"Better\">" +
                            "<Maintain Id=\"JKL\" />" +
                            "<Maintain Id=\"MNO\" />" +
                            "<Maintain Id=\"PQR\" />" +
                        "</Maintainance>" +
                        "<Maintainance Id=\"789\" Title=\"Bad\">" +
                            "<Maintain Id=\"STU\" />" +
                            "<Maintain Id=\"VWX\" />" +
                            "<Maintain Id=\"XYZ\" />" +
                        "</Maintainance>" +
                       "</Status>" +
                      "<Main Id=\"420\" Title=\"Failure\"/>" +
                      "<Project Id=\"220\" Title=\"First\"/>" +
                  "</Product>" +
                "</Root>";

            XDocument doc = XDocument.Parse(xml);
            List<XElement> maintainances = doc.Descendants("Maintainance").ToList();
            XElement product = doc.Descendants("Product").FirstOrDefault();
            product.ReplaceWith(new XElement("Product", product.Attributes()));
            product = doc.Descendants("Product").FirstOrDefault();
            foreach (XElement maintainance in maintainances)
            {
                product.Add(maintainance);
            }
        }
    }
}