开发者

C# -merge XML files but retain everything from the two files including version number

Suppose I have two XML files.

First XML File:

<?xml version="1.0"?>
    <AccessRequest xml:lang="en-US">
        <AccessLicenseNumber>Your_License</AccessLicenseNumber>
        <UserId>Your_ID</UserId>
        <Password&g开发者_运维百科t;Your_Password</Password>
    </AccessRequest>

Second XML File:

   <?xml version="1.0"?>
    <RatingServiceSelectionRequest xml:lang="en-US">
     <CustomerContext>Rating and Service</CustomerContext>
    </RatingServiceSelectionRequest>

Now, when I merge them two, i want the output like this:

<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
    <AccessLicenseNumber>Your_License</AccessLicenseNumber>
    <UserId>Your_ID</UserId>
    <Password>Your_Password</Password>
</AccessRequest>
<?xml version="1.0"?>
<RatingServiceSelectionRequest xml:lang="en-US">
  <CustomerContext>Rating and Service</CustomerContext>
</RatingServiceSelectionRequest>

When I am merging these two files using the usual methods found on google, the version number etc are being eaten up and RatingServiceSelectionRequest node ends up being part of AccessRequest node, which is clearly not what I want.

How do I approach this in C#.Net?

Note: The reason I want it like that is because this API I'm using is requesting it like that, so no two ways about it I guess.


You cannot have two top-level nodes

<AccessRequest>

and

<RatingServiceSelectionRequest>

in a well-formed XML file - that's by definition - a well-formed XML file has one and exactly one root node.

Also, you cannot have two XML processing instructions <?xml version="1.0"?> in a single file - most certainly not one in the middle of the file.

So you'll need to either introduce a new, "artificial" root, or stick on XML under the other root element or something.

If you really must keep that format, then you just concatenate the two files as strings:

string file1Contents = File.ReadAllText(@"file1.xml");
string file2Contents = File.ReadAllText(@"file2.xml");

File.WriteAllText(@"merged.xml", file1Contents + file2Contents);

No XML magic here ....


The "loss" of the the xml header (btw typicaly a desired feature), is probably the result of using XML/DOM aware methods to do the merging. Instead, by just reading the files as plain text, and writing them sequentially.

As Marc S points out, this will result in an invalid XML file, but if that's what you want/need...

In re-reading the question, the "complaint" about the content of the second file being inserted at the end of a node from the previous file, hint at your desire to keep a valid XML file, albeit one where the data from the second file is not integrated into this node. You'll therefore definitively need to introduce an extra node to serve as root node for the combined results. (at the moment these file both have their own root element).


This API looks familiar ;)

We use the same here and they do indeed require one right after the other. I'm assuming your also using the .xsd provided with the API to generate your request classes.

All that being said, we created a "helper" class that serializes both requests(access and rate) into a string, joins the string and posts the result as a byte array to the target url.

The breakout of our classes were:

Helper class -> serializes an object and returns it as a string(in this case an xml string)

Client class -> creates a byte array which is posted via WebRequest to the target system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜