Domanda

I need a good library to work with XML - Comparison/differences in .NET. I need a library to allow me to compare to xml files and then get the result as a XML file ,contains only the differences : example :

XML1:

< tag="gentype" value="Java"/>

XML2: < tag="gentype" value="C#"/>

Then the result should be something like: < tag="gentype" value="C#" status="changed"/>

È stato utile?

Soluzione

I would suggest to deserialize the 2 xml file into 2 objects and compare these 2 objects to find the difference. for example:

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

namespace ConsoleApplication5 {
    class Program {
        static void Main(string[] args) {
            var xml1 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <entity tag=\"gentype\" value=\"java\" />";
            var xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <entity tag=\"gentype\" value=\"c#\" />";

            var entity1 = DeserializeFrom(xml1);
            var entity2 = DeserializeFrom(xml2);

            if (entity1 != entity2) {
                entity1.Status = "changed";
            }

            var newxml = SerializeTo(entity1);
            Console.WriteLine(newxml);
            Console.Read();
        }

        static Entity DeserializeFrom(String xmlText) {
            var bytes = Encoding.UTF8.GetBytes(xmlText);
            using (var stream = new MemoryStream(bytes)) {
                var serializer = new XmlSerializer(typeof(Entity));
                return (Entity)serializer.Deserialize(stream);
            }
        }

        static String SerializeTo(Entity entity) {
            var bytes = new byte[1024];
            using (var stream = new MemoryStream(bytes)) {
                var serializer = new XmlSerializer(typeof(Entity));
                serializer.Serialize(stream, entity);
            }
            return Encoding.UTF8.GetString(bytes);
        }


    }

    [XmlRoot("entity")]
    public class Entity {
        [XmlAttribute("tag")]
        public String Tag { get; set; }

        [XmlAttribute("value")]
        public String Value { get; set; }

        [XmlAttribute("status")]
        public String Status { get; set; }

        public override bool Equals(object obj) {
            var entity = (Entity)obj;
            return this.Tag == entity.Tag && this.Value == entity.Value;
        }
    }
} 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top