简体   繁体   English

如何使用C#更新xml文件

[英]How to update xml file using C#

I have a requirement to update xml file by reading another xml file. 我需要通过读取另一个xml文件来更新xml文件。

eg: 例如:

Source.xml Source.xml

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="package1" version="1.0.26.0"/>
  <package id="package3" version="1.0.12.0"/>
  <package id="package4" version="1.0.40.0"/>
  <package id="package12" version="1.0.38.0"/>
  <package id="package6" version="1.0.8.0"/>
</packages>

Target.xml Target.xml

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="package1" version="1.0.2.0"/>
  <package id="package2" version="1.0.1.0"/>
  <package id="package3" version="1.0.4.0"/>
  <package id="package4" version="1.0.3.0"/>
</packages>

In the above example, "package1" exists in both the files. 在上面的示例中,两个文件中都存在“ package1”。 So the version of "package1" in Target.xml has to get update with the value in Source.xml. 因此,Target.xml中的“ package1”版本必须使用Source.xml中的值进行更新。

I tried the below code, but encountered exception during execution(no error during compilation): 我尝试了以下代码,但是在执行过程中遇到了异常(编译期间没有错误):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. 未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。

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

namespace XMLUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("In Execute..");
            string SourceFile = args[0];
            string TargetFile = args[1];
            string SourcePackageId = null;
            string SourcePackageVersion = null;
            XmlDocument SourceXmlDoc = new XmlDocument();
            XmlDocument TargetXmlDoc = new XmlDocument();
            SourceXmlDoc.Load(SourceFile);
            TargetXmlDoc.Load(TargetFile);
            XmlElement SourceRootElement = SourceXmlDoc.DocumentElement;
            //XmlElement SourceElement = SourceRootElement["packages"];
            XmlNodeList SourcexnList = SourceXmlDoc.SelectNodes("/packages");
            foreach (XmlNode Sourcexn in SourcexnList)
            {
                Console.WriteLine("In source loop..");
                SourcePackageId = Sourcexn["id"].InnerText;
                SourcePackageVersion = Sourcexn["version"].InnerText;
                Console.WriteLine("In source loop SourcePackageId.." + SourcePackageId);
                XmlElement TargetRootElement = TargetXmlDoc.DocumentElement;
                XmlNodeList TargetxnList = TargetXmlDoc.SelectNodes("/packages");
                foreach (XmlNode Targetxn in TargetxnList)
                {
                    Console.WriteLine("In Target loop..");
                    string TargetPackageId = Targetxn["id"].InnerText;
                    //string TargetPackageVersion = Targetxn["version"].InnerText;

                    if (SourcePackageId.Equals(TargetPackageId))
                    {
                        Targetxn["version"].InnerText = SourcePackageVersion;
                    }
                }
            }
        }
    }
}

Any help? 有什么帮助吗?

Thanks 谢谢

Try the following: 请尝试以下操作:

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

namespace XMLUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("In Execute..");
            string SourceFile = args[0];
            string TargetFile = args[1];
            string SourcePackageId = null;
            string SourcePackageVersion = null;
            XmlDocument SourceXmlDoc = new XmlDocument();
            XmlDocument TargetXmlDoc = new XmlDocument();
            SourceXmlDoc.Load(SourceFile);
            TargetXmlDoc.Load(TargetFile);
            XmlElement SourceRootElement = SourceXmlDoc.DocumentElement;
            //XmlElement SourceElement = SourceRootElement["packages"];
            XmlNodeList SourcexnList = SourceXmlDoc.SelectNodes("/packages");
            foreach (XmlNode Sourcexn in SourcexnList)
            {
                Console.WriteLine("In source loop..");
                foreach(XmlNode childS in Sourcexn.ChildNodes)
                {
                    SourcePackageId = childS.Attributes["id"].InnerText;
                    SourcePackageVersion = childS.Attributes["version"].InnerText;
                    Console.WriteLine("In source loop SourcePackageId.." + SourcePackageId);
                    XmlElement TargetRootElement = TargetXmlDoc.DocumentElement;
                    XmlNodeList TargetxnList = TargetXmlDoc.SelectNodes("/packages");
                    foreach (XmlNode Targetxn in TargetxnList)
                    {
                        Console.WriteLine("In Target loop..");
                        foreach (XmlNode childT in Targetxn.ChildNodes)
                        {
                            string TargetPackageId = childT.Attributes["id"].InnerText;
                            if (SourcePackageId.Equals(TargetPackageId))
                            {
                                childT.Attributes["version"].InnerText = SourcePackageVersion;
                            }
                        }
                    }
                }
            }
        }


    }
}

You need to check inside the ChildNodes of the xmlNode. 您需要检查xmlNode的ChildNodes内部。

This should work, just include System.Xml.Linq and System.Linq . 这应该起作用,只需include System.Xml.LinqSystem.Linq

var source = XDocument.Load("Source.xml");
var sPackages = source.Descendants("package");

var target = XDocument.Load("Target.xml");
var tPackages = target.Descendants("package");

foreach (var sPackage in sPackages)
{
    var tPackage = tPackages.FirstOrDefault(x => x.Attribute("id").Value == sPackage.Attribute("id").Value);

    if (tPackage != null && tPackage.Attribute("version").Value != sPackage.Attribute("version").Value)
    {
        target.Descendants("package").Single(x => x.Attribute("id").Value == sPackage.Attribute("id").Value)
                                     .SetAttributeValue("version", sPackage.Attribute("version").Value);
    }
}

target.Save("Target.xml");

This will update every target file's package version number that is also in the source file. 这将更新源文件中也包含的每个目标文件的软件包版本号。 You can easily add more functionality based on the above code. 您可以根据上述代码轻松添加更多功能。 As you see, Linq to XML is very clean and easy. 如您所见, Linq to XML非常干净和容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM