当所有节点具有相同名称时,如何获取特定节点值?

时间:2016-03-17 14:01:28

标签: c# xml

我的xml文件如下:

<xml number="1" Maintain="Yes">
<define count="0">
<Root Details="false">
  <Project 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> 
  </Project>
</Root> 

从上面的xml文件中我只需要维护节点的Id值并在另一个xml中打印,如下所示。

<Maintainance Id="123" Fields="ABC,DEF,GHI"/>
<Maintainance Id="456" Fields="JKL,MNO,PQR"/>
<Maintainance Id="789" Fields="STU,VWX,XYZ"/>

我已尝试了一些但无法获得正确的输出。使用xmldocument而不是xdocument对我来说更好。有人请帮助我。提前谢谢。

我使用了以下逻辑:

XmlDocument xmlDocument = new XmlDocument();
string Result = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\input.xml");
foreach (XmlNode MaintainNode in xmlDoc.SelectNodes("/Project/Maintainance"))
{
    if (MaintainNode.SelectSingleNode("Id").Equals("123"))
    {
        Result += Maintain.SelectSingleNode("Maintain").Attributes["Id"].Value;
    }
}
XmlElement MaintainanceElement = xmlDocument.CreateElement("Maintainance");
MaintainanceElement.SetAttribute("Id", 123);
MaintainanceElement.SetAttribute("Fields",string.Join(",", Result.Value.ToArray()));
xmlDocument.AppendChild(MaintainanceElement);
string xmlFile = @"C:\output.xml";
xmlDocument.Save(xmlFile);

1 个答案:

答案 0 :(得分:0)

试试xml linq。变量doc将包含修改后的结果。

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 =
                "<xml number=\"1\" Maintain=\"Yes\">" +
                    "<define count=\"0\"/>" +
                    "<Root Details=\"false\">" +
                      "<Project 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>" +
                      "</Project>" +
                    "</Root>" +
                   "</xml>";

            XDocument doc = XDocument.Parse(xml);

            List<XElement> maintainances = doc.Descendants("Maintainance").ToList();
            foreach (XElement maintainance in maintainances)
            {
                string[] ids = maintainance.Elements("Maintain").Select(x => x.Attribute("Id").Value).ToArray();
                maintainance.ReplaceWith(new XElement("Maintainance", new object[] {
                    maintainance.Attribute("Id"),
                    new XAttribute("Fields", string.Join(",", ids))
                }));
            }
        }
    }
}