Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#中用于读取节点和值的XML代码_C#_Xml - Fatal编程技术网

c#中用于读取节点和值的XML代码

c#中用于读取节点和值的XML代码,c#,xml,C#,Xml,我必须比较XML中的两个文件,在XML中,我需要显示文件之间“不匹配模式”的内容,无论是节点名称的差异还是节点值的差异 到目前为止,我编写的代码是 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.XPath; namespace ConsoleApplication1 { public class

我必须比较XML中的两个文件,在XML中,我需要显示文件之间“不匹配模式”的内容,无论是节点名称的差异还是节点值的差异

到目前为止,我编写的代码是

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

namespace ConsoleApplication1
{
   public class Program
    {
        public Program()
        {

            XmlDocument _mxml1 = new XmlDocument();

            _mxml1.Load(@"C:\Users\Rahul\Documents\xml work\abcd.xml");

            XmlDocument _mxml2 = new XmlDocument();

            _mxml2.Load(@"C:\Users\Rahul\Documents\xml work\efg.xml");


            XmlElement _melement1 = _mxml1.DocumentElement;

            XmlElement _melement2 = _mxml2.DocumentElement;


            XPathNavigator nav1 = _mxml1.CreateNavigator();

            XPathNavigator nav2 = _mxml2.CreateNavigator();

            if (_melement1.HasChildNodes && _melement2.HasChildNodes && _melement1.ChildNodes.Count == _melement2.ChildNodes.Count)
            {

                XmlNodeList _node1 = _melement1.ChildNodes;

                XmlNodeList _node2 = _melement2.ChildNodes;

                foreach (XmlNode _n1 in _node1)
                {

                    foreach (XmlNode _n2 in _node2)
                    {

                        int count = _n1.ChildNodes.Count;

                        int count2 = _n2.ChildNodes.Count;


                        if (_n1.Name.ToString() == _n2.Name.ToString())
                        {

                            int res = _n1.OuterXml.CompareTo(_n2.OuterXml);

                            if (res == 1)
                            {

                                Console.WriteLine("All the child names match for the case ");

                            }

                            if (_n1.InnerText.ToString() != _n2.InnerText.ToString())
                            {



                                Console.WriteLine("Values of some child doesnot match");



                                // here we need to traverse through child nodes of both xmls to find out the change in file..



                            }

                            if (_n1.InnerXml.ToString() == _n2.InnerXml.ToString())
                            {

                                Console.WriteLine("Records match completly");


                            }


                        }

                        else
                        {

                            Console.WriteLine("XML_1 " + "     " + "" + "XML_2  \n" + _n1.Name + " " + _n2.Name);

                        }

                    }

                }

            }


        }
        static void Main(string[] args)
        {
            Program obj = new Program();
        }
    }
}

现在的问题是,如果一个节点的值存在差异,那么如何在两个文件中找到不同的字符串部分…

您是否考虑过格式化代码并删除空行?看,我不明白您的意思