C# XML Merging (2+ files) - c#

I am having problem to merge 2 or more xml files into 1 using c#.
I am doing it with DataSets:
//ds1,ds2,ds3 are DataSets
private void MyMethod()
{
ds1.ReadXml(tmpStream);
ds2.ReadXml(tmpStream);
ds1.Merge(ds2);
}
but i dont want to use DataSet. i am searching for another way.
first XML is
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>
the second is
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>
result must be
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
<item>
<path>'filePath'</path>
<deleted>0</deleted>
<date>9/23/2010 11:30:03 AM</date>
</item>
</catalog>

Though this isn't really clear of what sort of merge you want, this article Merging XML Files, Schema Validation, and More might help you get the idea.
Easiest could be, if you dont want any checks to be performed(duplicates, zombies, etc)
var ResultXml = XDocument.Load("file1.xml");
ResultXml.Root.Add(XDocument.Load("file2.xml").Root.Elements());

To merge XML files into resulting one, you could use Microsoft's XML Diff and Patch C# API. You could read more about it in Eric White's blog post: "OpenXmlDiff.Exe: A Utility to Find the Differences Between Two Open XML Documents"

Related

Removing Attribute value based on value from an XML using VB.Net

I have an XML as below
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns="http://com/uhg/uht/uhtSoapMsg_V1"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
<uhtHeader
xmlns="http://com/uhg/uht/uhtHeader_V1">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="http://com/uhg/uht/getClaimHistory_V24">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I want to remove all the attribute values with "http" like below:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope
xmlns=""
xmlns:env="">
<env:Header>
<uhtHeader
xmlns="">
<consumer>COMET</consumer>
<auditId></auditId>
<sendTimestamp>2020-09-03T18:15:40.942-05:00</sendTimestamp>
<environment>P</environment>
<businessService version="24">getClaimHistory</businessService>
<status>success</status>
</uhtHeader>
</env:Header>
<env:Body>
<srvcRspn
xmlns="">
<srvcErrList arrayType="srvcErrOccur[1]" type="Array">
<srvcErrOccur>
<orig>Foundation</orig>
<rtnCd>00</rtnCd>
<explCd>000</explCd>
<desc></desc>
</srvcErrOccur>
</SrvcErrList>
</srvcRspn>
</env:Body>
</env:Envelope>
I have tried several ways but none of them has worked for me. Can anyone suggest what is fastest way to do it in VB.NET/C#.
The actual response is very large (approx 100000 lines of XML minimum) and using for each will consume a good amount of time. Is there any parsing method or LINQ query method which can do it faster.
I got the way to do it using Regex as below:
Return Regex.Replace(xmlDoc, "((?<=<|<\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?="".*?""", "")
It serves my purpose completely. Thanks Cleptus for your quick reference.

Get and Set XML elements with Namespaces

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";

Adding an XML file to your XNA project

I'm creating an XNA game. I've made it so I can specify all the level details in an XML file which is then de-serialized and used to set up the level details.
At the moment, it's just referencing a file on my computer - my question is, how do I reference this more generically?
Adding the xml in my content folder created a multitude of complaints about schemas and such like, which made me think that likely wasn't the correct route.
Any suggestions?
I tried removing all the entries from the XNA, this gives:
Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)
EDIT:
The xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type = "RDrop.Level[]">
<Item>
(stuff)
</Item>
<Item>
(stuff)
</Item>
</Asset>
</XnaContent>
EDIT:
I've started a new windows phone project - the previous one wasn't one. I've copied everything over and added "dataTypes" ala this tutorial:
http://msdn.microsoft.com/en-us/library/ff604979.aspx
Game project references -> content, MyDataTypes.
Content references -> MyDataTypes.
The XML is as is in previous edit and is contained in the content folder via Add-> Existing Item-> Level.XML.
Any ideas?
You can leave the build action as "Compile". One method to do what you want is the following:
Create a class that the xml is going to be describing. Example: Level.cs
Then structure your xml file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level">
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Asset>
</XnaContent>
if you want the xml to describe an array of objects you can do structure the xml like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XnaContent>
<Asset Type="The_Level_class_namespace.Level[]">
<Item>
<Property1>Value</Property1>
<Property2>Value</Property2>
<Property3>Value</Property3>
<Property4>Value</Property4>
</Item>
</Asset>
</XnaContent>
From there you just need to make sure your values are in the proper format. For example a vector2 object would be like this:
<Vector2Property>x_value y_value</Vector2Property>
Make sure that your content project references the game project or library project.
Hope this helps :)
Open the properties of your XML document (right click in your content folder). You can set the Build Action to : None.
That way, the compiler won't analyse your schema, thus it won't produce any warnings.
(I'm not entirely sure about this, just my first guess)

How do I identify duplicate nodes in XPath 1.0 using an XPathNavigator to evaluate?

I am trying to identify duplicate serial numbers from the following xml using XPath 1.0 and then evaluating it in .Net using an XPathNavigator.
<?xml version="1.0" encoding="utf-16"?>
<Inventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Item>
<SerialNumber>1111</SerialNumber>
</Item>
<Item>
<SerialNumber>1112</SerialNumber>
</Item>
<Item>
<SerialNumber>1112</SerialNumber>
</Item>
</Items>
</Inventory>
I tried to do this by evaluating this
//Items/Item/SerialNumber
expression in a custom XSLT Context Function (implementing IXsltContextFunction like this MSDN example) in .Net but the Invoke function gets called one result at a time so I have no visibility of the other results to find duplicates.
1) Is there a way of doing this using a single XPath 1.0 expression?
OR
2) Is there a way of passing in an array of elements into a single Invoke call of the custom XSLT Context Function class? I'm working in VB.Net but am happy with any C# examples anyone can share.
Thanks,
Gavin
Edit
Thanks to O R Mapper and Dimitre for their responses. I initially accepted O R Mapper's response since it did do what I asked. I've since accepted Dimitre's answer since I like that it provides a distinct list of values. Both responses very helpful though!
I'm going to answer 1), so 2) should not matter any more:
You can use the preceding-sibling axis on your <Item> elements to find any preceding <Item> elements with the same serial number.
Try this (written so that it returns only the serial numbers themselves rather than elements - if this is not quite what you want, and you don't know how to change the result, let me know):
/Inventory/Items/Item/SerialNumber/node()[.=../../preceding-sibling::Item/SerialNumber/node()]
For your sample document, it returns
1112
Use:
/*/*/Item
[SerialNumber = following-sibling::Item/SerialNumber
and
not(SerialNumber = preceding-sibling::Item/SerialNumber)
]
This selects just one Item element for any group of Item elements that have a SerialNumber child with the same string value.
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/*/Item
[SerialNumber = following-sibling::Item/SerialNumber
and
not(SerialNumber = preceding-sibling::Item/SerialNumber)
]"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on this XML document (based on the provided one, but made more interesting):
<Inventory>
<Items>
<Item>
<SerialNumber>1111</SerialNumber>
</Item>
<Item>
<SerialNumber>2222</SerialNumber>
</Item>
<Item>
<SerialNumber>2222</SerialNumber>
</Item>
<Item>
<SerialNumber>2222</SerialNumber>
</Item>
<Item>
<SerialNumber>1111</SerialNumber>
</Item>
<Item>
<SerialNumber>1111</SerialNumber>
</Item>
<Item>
<SerialNumber>3333</SerialNumber>
</Item>
</Items>
</Inventory>
the transformation evaluates the XPath expression and copies the selected nodes to the output:
<Item>
<SerialNumber>1111</SerialNumber>
</Item>
<Item>
<SerialNumber>2222</SerialNumber>
</Item>
Finally, if you want to get just the SerialNumber duplicate values, use:
/*/*/Item
[SerialNumber = following-sibling::Item/SerialNumber
and
not(SerialNumber = preceding-sibling::Item/SerialNumber)
]
/SerialNumber/text()

How to read nested XML using xDocument in Silver light?

Hi currently I have a nested XMl , having the following Structure :
<?xml version="1.0" encoding="utf-8" ?>
<Response>
<Result>
<item id="something" />
<price na="something" />
<?xml version="1.0" encoding="UTF-8" ?>
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
</Result>
<NumberReturned>10</NumberReturned>
<TotalMatches>10</TotalMatches>
</Response>
Any help on how to read this using Xdocument or XMLReader will be really helpfull.
Thanks,
Subhendu
XDocument and XmlReader are both XML parsers that expect a properly formed XML as input. What you have shown is not a XML file. So the first task would be to extract the nested XML and as this is not valid XML you cannot rely on any parser to do this job. You'll need to resort to string manipulation and or regular expressions.
My suggestion would be to fix the procedure generating this invalid XML in the first place. Another suggestion is to never generate a XML file manually but use an appropriate tool for this (XmlWriter, XDocument, ...)

Categories