比较两个XML并获得差异 [英] Compare two XML and Get the difference

查看:118
本文介绍了比较两个XML并获得差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较具有共同结构的两个XML文件并将差异存储在List中。



I want to compare two XML file having the common structure and store the difference in a List.

List<variance> ListVariance= new List<variance>();

ListVariance=GetDifference(string OldXml,String NewXml);

class Variance
{
    public string Prop { get; set; }
    public object valA { get; set; }
    public object valB { get; set; }
}





请注意样本XML:



OldXml:



Please Note the sample XML:

OldXml:

<pre lang="HTML">

<CredoRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <ProductIdentification>
          <MaterialId>0</MaterialId>
          <MaterialName>CONTACT ADHESIVE SEPTONE AEROSOL</MaterialName>
          <IssueDate>26/10/2015</IssueDate>
          <InitialDate>26/10/2015</InitialDate>
          <Uses>
               <UserDefinedUses>
                    <UserDefinedUses>
                         <Id>0</Id>
                         <CountryId>0</CountryId>
                         <LanguageId>0</LanguageId>
                         <IsDefault>false</IsDefault>
                    </UserDefinedUses>
               </UserDefinedUses>
          </Uses>
          <ReachUseses />
          <MsdsId>0</MsdsId>
     </ProductIdentification>
     <CreditoPosteri>
          <Ingredients>
               <IngredientCredo>
                    <MaterialId>3219162</MaterialId>
                    <Name>water</Name>
                    <Proportion>50</Proportion>
                    <CWNumber>10000</CWNumber>
                    <CAS>7732-18-5</CAS>
               </IngredientCredo>
          </Ingredients>
          <PureIngredients>
               <IngredientCredo>
                    <MaterialId>3219162</MaterialId>
                    <Name>water</Name>
                    <Proportion>80</Proportion>
                    <CWNumber>10000</CWNumber>
                    <CAS>7732-18-5</CAS>
               </IngredientCredo>
          </PureIngredients>
          <StateCode>#00Liquid</StateCode>
          <WaterSolubilityCode>#01immiscible</WaterSolubilityCode>
          <PH>Not Available</PH>
          <PHAsASolution>Not Available</PHAsASolution>
          <FlashPoint>Not Available</FlashPoint>
          <SpecificGravity>Not Available</SpecificGravity>
          <LowerExplosiveLimit>Not Available</LowerExplosiveLimit>
          <UpperExplosiveLimit>Not Available</UpperExplosiveLimit>
          <BoilingPointRange>Not Available</BoilingPointRange>
          <MeltingPointRange>Not Available</MeltingPointRange>
          <AutoIgnitionTemperature>Not Available</AutoIgnitionTemperature>
          <DecompositionTemperature>Not Available</DecompositionTemperature>
          <Volatiles>Not Available</Volatiles>
          <MolecularWeight>Not Available</MolecularWeight>
          <RelativeVepourDensity>Not Available</RelativeVepourDensity>
          <VapourPreassure>Not Available</VapourPreassure>
          <Viscosity>Not Available</Viscosity>
          <EvaporationRateUnit>BuAC = 1</EvaporationRateUnit>
          <TotalVoc>Not Available</TotalVoc>
          <AutoSanitised>true</AutoSanitised>
     </CreditoPosteri>
     <ManufacturersDetails>
          <NID>5345</NID>
          <Address>44 Aquarium Avenue, Hemmant, QLD, Australia</Address>
          <CountryId>0</CountryId>
          <Telephone1>+61 7 3390 5044</Telephone1>
          <Emergency1>+61 7 3821 0623</Emergency1>
          <Fax>+61 7 3390 5041</Fax>
          <Email>general@septone.com.au</Email>
          <WebSite>http://www.septone.com.au/</WebSite>
          <IsUser xsi:nil="true" />
     </ManufacturersDetails>
     <DangerousGoods>
          <PackagingGroup>None</PackagingGroup>
          <UN>None</UN>
          <DGC>None</DGC>
          <SubRisk1>None</SubRisk1>
          <SubRisk2>None</SubRisk2>
     </DangerousGoods>
     <ToxicityIrritation>
          <ToxicityDataList />
     </ToxicityIrritation>
     <CandLActive>false</CandLActive>
     <PublishMaterial>false</PublishMaterial>
     <P_Code>[]</P_Code>
</CredoRequest>







NewXML:







<CredoRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <ProductIdentification>
          <MaterialId>0</MaterialId>
          <MaterialName>CONTACT ADHESIVE SEPTONE AEROSOL</MaterialName>
          <IssueDate>26/10/2015</IssueDate>
          <InitialDate>26/10/2015</InitialDate>
          <Uses>
               <UserDefinedUses>
                    <UserDefinedUses>
                         <Id>0</Id>
                         <CountryId>0</CountryId>
                         <LanguageId>0</LanguageId>
                         <IsDefault>false</IsDefault>
                    </UserDefinedUses>
               </UserDefinedUses>
          </Uses>
          <ReachUseses />
          <MsdsId>0</MsdsId>
     </ProductIdentification>
     <CreditoPosteri>
          <Ingredients>
               <IngredientCredo>
                    <MaterialId>3219161</MaterialId>
                    <Name>water</Name>
                    <Proportion>50</Proportion>
                    <CWNumber>1000</CWNumber>
                    <CAS>7732-18-5</CAS>
               </IngredientCredo>
          </Ingredients>
          <PureIngredients>
               <IngredientCredo>
                    <MaterialId>3219162</MaterialId>
                    <Name>water</Name>
                    <Proportion>50</Proportion>
                    <CWNumber>10000</CWNumber>
                    <CAS>7732-18-5</CAS>
               </IngredientCredo>
          </PureIngredients>
          <StateCode>#00Liquid</StateCode>
          <WaterSolubilityCode>#01immiscible</WaterSolubilityCode>
          <PH>Not Available</PH>
          <PHAsASolution>Not Available</PHAsASolution>
          <FlashPoint>Not Available</FlashPoint>
          <SpecificGravity>Not Available</SpecificGravity>
          <LowerExplosiveLimit>Not Available</LowerExplosiveLimit>
          <UpperExplosiveLimit>Not Available</UpperExplosiveLimit>
          <BoilingPointRange>Not Available</BoilingPointRange>
          <MeltingPointRange>Not Available</MeltingPointRange>
          <AutoIgnitionTemperature>Not Available</AutoIgnitionTemperature>
          <DecompositionTemperature>Not Available</DecompositionTemperature>
          <Volatiles>Not Available</Volatiles>
          <MolecularWeight>Not Available</MolecularWeight>
          <RelativeVepourDensity>Not Available</RelativeVepourDensity>
          <VapourPreassure>Not Available</VapourPreassure>
          <Viscosity>Not Available</Viscosity>
          <EvaporationRateUnit>BuAC = 1</EvaporationRateUnit>
          <TotalVoc>Not Available</TotalVoc>
          <AutoSanitised>true</AutoSanitised>
     </CreditoPosteri>
     <ManufacturersDetails>
          <NID>5345</NID>
          <Address>44 Aquarium Avenue, Hemmant, QLD, Australia</Address>
          <CountryId>0</CountryId>
          <Telephone1>+61 7 3390 504</Telephone1>
          <Emergency1>+61 7 3821 0623</Emergency1>
          <Fax>+61 7 3390 5041</Fax>
          <Email>general@septone.com.au</Email>
          <WebSite>http://www.septone.com.au/</WebSite>
          <IsUser xsi:nil="true" />
     </ManufacturersDetails>
     <DangerousGoods>
          <PackagingGroup>None</PackagingGroup>
          <UN>None</UN>
          <DGC>None</DGC>
          <SubRisk1>None</SubRisk1>
          <SubRisk2>None</SubRisk2>
     </DangerousGoods>
     <ToxicityIrritation>
          <ToxicityDataList />
     </ToxicityIrritation>
     <CandLActive>false</CandLActive>
     <PublishMaterial>false</PublishMaterial>
     <P_Code>[]</P_Code>
</CredoRequest>

推荐答案

I think this article might be interesting for you: Comparing DataSets using LINQ[^]



To read the XML data into data sets you can do like this:

I think this article might be interesting for you: Comparing DataSets using LINQ[^]

To read the XML data into data sets you can do like this:
DataSet dsOld = new DataSet("Old");
dsOld.ReadXml(@"XMLFile1.xml", XmlReadMode.InferSchema);
dsOld.AcceptChanges();

DataSet dsNew = new DataSet("New");
dsNew.ReadXml(@"XMLFile2.xml", XmlReadMode.InferSchema);
dsNew.AcceptChanges();


If you own the schema. then maybe you make DSL (Domain Specific Language) and use (embed) PowerShell CliXML and once you have an object then you can play as much as you wish!
If you own the schema. then maybe you make DSL (Domain Specific Language) and use (embed) PowerShell CliXML and once you have an object then you can play as much as you wish!


这篇关于比较两个XML并获得差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆