如何用最新更新xml值?

时间:2015-11-27 11:07:37

标签: c# xml

我有如下的input.xml文件。

<Root>
      <Main Name="Install">
        <Details>Success</Details>
        <Maintain>Install period</Maintain>
      </Main>
    <Main Name="Uninstall">
        <Details>failure</Details>
        <Maintain>uninstall period</Maintain>
      </Main>
     <Main Name="Discard">
        <Details>failure</Details>
        <Maintain>discard period</Maintain>
      </Main>
     <Main Name="Install">
        <Details>Done</Details>
        <Maintain>Got Output</Maintain>
      </Main>
    </Root>

我使用了这个input.xml,并使用以下代码将其写在output.xml上。

XmlDocument xml = new XmlDocument();
xml.Load(@"C:\input.xml");
XmlNodeList MainNameList = xml.SelectNodes("/Root/Main");
XmlNodeList MainDetailsList = xml.SelectNodes("/Root/Main/Details");
XmlNodeList MainMaintainList = xml.SelectNodes("/Root/Main/Maintain");
for (int count = 0; count < MainNameList.Count; count++)
{
    string mainName = MainNameList[count].Attributes[0].Value;
    string mainDetails = MainDetailsList[count].InnerText;
    string mainMaintain = MainMaintainList[count].InnerText;
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml("<Root></Root>");
    XmlNode MainNode = xmldoc.SelectSingleNode("Root").AppendChild(xmldoc.CreateElement("Main"));
    MainNode.Attributes.Append(xmldoc.CreateAttribute("Name")).Value = mainName;
    MainNode.AppendChild(xmldoc.CreateElement("Details")).InnerText = mainDetails;
    MainNode.AppendChild(xmldoc.CreateElement("Maintain")).InnerText = mainMaintain;
}
xmldoc.Save(@"C:\output.xml");

但我需要输出xml如下:

<Root>
 <Main Name="Uninstall">
   <Details>failure</Details>
    <Maintain>uninstall period</Maintain>
 </Main>
 <Main Name="Discard">
    <Details>failure</Details>
    <Maintain>discard period</Maintain>
 </Main>
<Main Name="Install">
    <Details>Done</Details>
    <Maintain>Got Output</Maintain>
 </Main>
</Root>

即。我只需要Main元素的最新条目。与所有元素的相同,最新元素的详细信息只应输入output.xml文件中。帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

我个人认为来自XDocument命名空间的System.Xml.LinqXmlDocument更易于操作。下面的代码可以满足您的需求(我认为它非常不言自明):

        XDocument xDoc1 = XDocument.Load(inputFileName);

        var elems = xDoc1.Element("Root").Elements("Main");

        XElement xInstall = elems.LastOrDefault(a => a.Attribute("Name").Value == "Install");
        XElement xUninstall = elems.LastOrDefault(a => a.Attribute("Name").Value == "Uninstall");
        XElement xDiscard = elems.LastOrDefault(a => a.Attribute("Name").Value == "Discard");

        XDocument xdoc2 = new XDocument();

        xdoc2.Add(
            new XElement("Root", new XElement[]
            {
                xInstall,
                xDiscard,
                xUninstall
            })
        );

        xdoc2.Save(ouputFileName);

答案 1 :(得分:0)

如果我理解正确,并且您希望每个最后一个主要元素具有不同的name属性,那么这应该适合您。您只需要更改输入和输出元素的路径

try
{
       const string uninstallName = "Uninstall";
       const string installName = "Install";
       const string discardName = "Discard";
       const string nameAttribute = "Name";
       const string mainElement = "Main";
       const string rootElement = "Root";

       XDocument xdoc = XDocument.Load("../../input.xml");
       XElement root = xdoc.Root;
       if (root == null)
           throw  new Exception("No root element found");
       var mains = root.Elements(mainElement);
       if (!mains.Any())
           throw new Exception(String.Format("No element named {0} was found", mainElement));
       var lastInstall = mains.LastOrDefault(m => m.Attribute(nameAttribute).Value == installName);
       var lastDiscard = mains.LastOrDefault(m => m.Attribute(nameAttribute).Value == discardName);
       var lastUninstall = mains.LastOrDefault(m => m.Attribute(nameAttribute).Value == uninstallName);

       XDocument output = new XDocument();
       output.Add(new XElement(rootElement));
       if (lastUninstall != null)
           output.Root.Add(lastUninstall);
       if (lastDiscard != null)
           output.Root.Add(lastDiscard);
       if (lastInstall != null)
           output.Root.Add(lastInstall);
       output.Save("output.xml");

}
catch (Exception ex)
{
     // log, write to output or anything
}