I want to compare two xml raw files and capture their differences in data. For example,
<Segment>
<SegNum>2</SegNum>
<Dt>20110910</Dt>
<Comment>B test</Comment>
</Segment>
</Retrieve>
</info>
File B:
<info>
<Retrieve>
<LastNameInfo>
<LNameNum>2</LNameNum>
<NameType/>
<LName>TESTING</LName>
</LastNameInfo>
<Segment>
<SegNum>1</SegNum>
<Status>HS</Status>
<Comment>A test</Comment>
</Segment>
<Segment>
<SegNum>2</SegNum>
<Dt>20110</Dt>
<Comment>COULD I USE LINQ</Comment>
</Segment>
</Retrieve>
</info>
so the output i need should be a file with just the differences like
<LNameNum>1</LnameNum> File 2:<Lnamenum>2</Lnamenum>
There is a library (and tool), XML Diff and Patch, published to MSDN a while back that does this.
I've used the XmlDiff class in the past. At last check, it still works with framework 4.0.
http://msdn.microsoft.com/en-us/library/aa302295.aspx
Related
Currently I am working on generate a form (WinForm) programmatically according to the given format file. This feature is used for generating forms like product specifications.
The given format file is a text file, its extension is unknown yet, it could be .txt. This format file might change in the future according to the needs, for now, it looks like:
[Hardware]
[Voltage]
[Range]
[Accuracy]
[Current]
[Range]
[Time]
[Precision]
[Software]
[Set up]
[A]
[B]
[C]
[Require]
[E]
[F]
...
And my program should read this format file and generate a form accordingly, like:
This form would later be filled with data from a given Excel file, but I can worry about this part later, for now I only need to worry about how to read from the format file and generate a form.
Any tip would be super helpful. Thank you in advance!
Edit 1: the format file can be anything, I am using .xml file for the format now:
<regionList>
<region id="1">
<regionName>Hardware</regionName>
<modules>
<module1>Voltage</module1>
<components>
<component1>Range</component1>
<component2>Accuracy</component2>
</components>
<module2>Current</module2>
<components>
<component1>Range</component1>
<component2>Time</component2>
<component3>Precision</component3>
</components>
</modules>
<\region>
<region id="2">
<regionName>Software</regionName>
<modules>
<module1>Set Up</module1>
<components>
<component1>A</component1>
<component2>B</component2>
<component3>C</component3>
</components>
<module2>Require</module2>
<components>
<component1>D</component1>
<component2>E</component2>
</components>
</modules>
<\region>
...
<\regionList>
I am building an application in which the output XML file should look something like this.
<xml file>
<Session>
<Machine name= XXX>
<date = ####>
<Recorded Speech= XXXXXXXX>
<Capture= picture.jpeg>
</Capture>
</Recorded Speech>
</date>
</Machine>
</Session>
<Session>
<Machine name= XXX>
<date = ####>
< Recorded Speech= XXXXXXXX>
<Capture= picture.jpeg>
</Capture>
</Recorded Speech>
</date>
</Machine>
</Session>
</xml file>
and so on...
I am able to output all the data specified in the tags separately but not able to integrate them into a single XML file.
Also, I want to maintain the sessions. The data which is there in the XML is captured during run time.
I am trying to build this in WPF and using MVVM architecture.
NEWBIE QUESTION.
I haven't worked that much with xml, nothing like this anyway. I have some XML as shown below that I receive which has several namespaces.
I need to read some values, then update others before returning the revised XML with namespaces intact - don't want them removed.
I am given the path to some of the elements like this cred/sub/aa or trip/items/item[0]/customerInfo/custName.
But it seems that namespaces make it difficult to get to those elements so simply.
Does anybody know how I can read some of the values like NON-SMOKING from custPref or get the value CABBAGE from bossman/zz.
Also, I want to be able to then set a value such as custName to say Mr. X.
Any ideas?
Thanks.
<?xml version="1.0" encoding="utf-16" ?>
<A1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<cred xmlns="https://blah-blah.com/?foobar">
<sub>
<aa>Zippo</aa>
<bb>lighter</bb>
</sub>
<reqId>
<cc></cc>
<dateOfBirth></dateOfBirth>
</reqId>
</cred>
<reqName xmlns="http://blah-blah/vader/base">qwerty</reqName>
<reqId xmlns="http://blah-blah/vader/base">12345</reqId>
<machine xmlns="http://blah-blah/vader/base">
<qqq>hello</qqq>
<www>goodbye</www>
<eee>99999</eee>
<rrr>88888</rrr>
</machine>
<monkey xmlns="http://blah-blah/vader/base">alskdjfhg</monkey>
<math xmlns="http://blah-blah/vader/base">
<language></language>
</math>
<trip xmlns="http://blah-blah/simple">
<tripOverview xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
</tripOverview>
<bossman xmlns="http://blah-blah/vader/base">
<zz>CABBAGE</zz>
<yy>BANANA</yy>
<xx>MELON</xx>
<ww>SYRUP</ww>
</bossman>
<items>
<item>
<itemSummary xmlns="http://blah-blah/vader/base">
<description></description>
<cost></cost>
<reference></reference>
</itemSummary>
<customerInfo xmlns="http://blah-blah/vader/base">
<custName></custName>
<custPref>NON-SMOKING</custPref>
</customerInfo>
<seatId xmlns="http://blah-blah/vader/base">1</seatId>
</item>
</items>
</trip>
</A1>
string xml = "<Root><Options></Options></Root>";
var xdocs = XDocument.Parse(xml);
xdocs.Descendants().Where(q => q.Name == "Options").FirstOrDefault().Value = "FoundIt";
I wrote several lines of code but still can't get over this:
I need to load many xml docs from web library. I don't know how many documents there are so I wonder which loop should I use while loading:
XDocument doc = XDocument.Load("http://" + i);
where -i is identifiers number.
I tried loading until i get document without meaningful content (thought it is the end, the rest are empty), but problem is that there is several Xdocs that are empty in the middle of library.
XML with content looks like
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
<GR>
<header>
<identifier>53,number of doc...used for counting</identifier>
</header>
<metadata>
<P xmlns="" xsi:schemaLocation="">
<TITLE>title</TITLE>
<CERTIFICATE NAME="different names">
</CERTIFICATE>
<YEAR>
<DATE>2012-10-18T00:00:00Z</DATE>
</YEAR>
<MINIATURE>
<COPY>
<CNAME>Copy name<CNAME>
<FORMAT>obj/max/dxf/3ds/...</FORMAT>
</COPY>
</MINIATURE>
</metadata>
</GR>
</OP>
XML without content
<?xml version="1.0" encoding="utf-8"?>
<OP xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<request verb="GR" identifier="53" metadataPrefix="p"></request>
Furthermore, I need to do some counting like:
Tot.no. of doc,
No. of docs per certificate <CERTIFICATE>
No. of docs for each year <YEAR><DATE>
No of docs for each format <MINIATURE><COPY><FORMAT>
and my output should look like:
<?xml version="1.0" encoding="UTF-8" ?>
<Statistic>
<DocSum>21220</DocSum>
<Certificates>
<Certificate id=”certificateName”>17098</Certificate>
…
<Certificates>
<Years>
<Year year=”2014”>23</Year>
…
</Years>
<Miniature>
<Format post=”obj”>11723</Format>
…
</Miniature>
</Statistic>
If you could give me some help, hints or tips how to deal with it.
The posted answer by smink to the following thread should get you on the right path.
C# HttpWebRequest command to get directory listing
One of the easiest ways to get a list of the files of a web directory without knowing exactly how many there are or their filenames is by parsing the html of the directory and pulling out the tags.
You can then iterate through these tags and filter them out for the files by extensions that you need. I can provide a more in-depth example if necessary.
Here is a part of my xml file
<teams>
<team-profile>
<name>Australia</name>
<id>1</id>
<stats type="Test">
<span>1877-2010</span>
<matches>721</matches>
<won>339</won>
<lost>186</lost>
<tied>2</tied>
<draw>194</draw>
<percentage>47.01</percentage>
</stats>
<squad>
<player id="135" fullname="Shane Warne"/>
<player id="136" fullname="Damien Martyn"/>
<player id="138" fullname="Michael Clarke"/>
</squad>
</team-profile>
</team>
I have read somewhere that there is a way to save this XML directly to database. I am using VS2010. I have created the dataset, for the data i need from this xml. Is there any way to map this XML directly on dataset?
Any other idea?
I also have to save some other more complex XML files to database.
I have tried xsd.exe to create xsd schema for this XML.
SQL Server 2005+ has a native XML datatype, if you create a column, you can simply do an insert into that column, here is an insert similar to yours, this table's second column's datatype is xml
INSERT INTO docs VALUES (1, '<book genre="security"
publicationdate="2002" ISBN="0-7356-1588-2">
<title>Writing Secure Code</title>
<author>
<first-name>Michael</first-name>
<last-name>Howard</last-name>
</author>
<author>
<first-name>David</first-name>
<last-name>LeBlanc</last-name>
</author>
<price>39.99</price>
</book>')
INSERT INTO docs VALUES (2,
'<doc id="123">
<sections>
<section num="1"><title>XML Schema</title></section>
<section num="3"><title>Benefits</title></section>
<section num="4"><title>Features</title></section>
</sections>
</doc>')