Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to compare two XML file having the common structure and store the difference in a List.

C#
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; }
}


Please Note the sample XML:

OldXml:
HTML
<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:


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>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>
Posted
Comments
xszaboj 23-Nov-15 4:44am    
and what exactly is your problem?
Sinisa Hajnal 23-Nov-15 6:12am    
What have you tried? It seems you have good idea what needs to be done.
Anadi Chakraborty 23-Nov-15 11:51am    
No sir I don't have any idea..I am a student..try to have some idea from people like you.
[no name] 23-Nov-15 9:14am    
can you share more details on the your facing issue
Matt T Heffron 23-Nov-15 21:31pm    
This is basically the same question you asked 3 days ago, except then it was comparing classes, this time it is XML. You can apply the advice you got there to this. Just load the XML into some classes, even the classes of System.Xml would work (XmlDocument.Load()), then compare the classes as before.
I did notice that UserDefinedUses nests on itself, this is probably not a good idea. Should it be
<UserDefinedUses>
 <UserDefinedUse> <!-- singular, not plural -->
 </UserDefinedUse>
</UserDefinedUses>

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:
C#
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();
 
Share this answer
 
v2
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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900