Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm using two third-party components, each with their own built-in serializer that will write .xml to stream, or file.

I want to save one file that contains both .xml streams, but does not "merge" them ... in the sense of "combining" them where the difference between the two files is "lost."

Obviously, I could append one file to the other, and write some kind of delimiter string so that, in parsing, I could distinguish the two files, but, I suspect there's a simpler solution using LinqToXml, or XSLT, or something ... hence: this question.

At run-time I will want to read the combined file, and quickly create two .xml streams each of which can be "fed" to the two components, so they will be "re-constituted."

I'd appreciate general advice on strategy here; it's been a long while since I've messed around with .xml.

thanks, Bill
Posted

1 solution

You could try to create a third structure with the two other structures as elements.
Not exactly sure this is what you want.

XML
<combined>
  <files>
    <file id="1">
      <data>Some data in file 1</data>
    </file>
    <file id="2">
      <content>Something for file 2</content>
    </file>
  </files>
</combined>


This will preserve the existing structure and you can save the file as one or as different parts by using e.g. XElement.

C#
string combinedData = "<combined><files><file id=\"1\"><data>Some data in file 1</data></file><file id=\"2\"><content>Something for file 2</content></file></files></combined>";

// Use XDocument.Load() to read from file
XDocument xdoc = XDocument.Parse(combinedData);
XElement xeFiles = xdoc.Root.Element("files");


XElement xeFile1 = xeFiles.Elements("file").Where(x => x.Attribute("id").Value == "1").FirstOrDefault();
string file1Content = xeFile1.FirstNode.ToString();

XElement xeFile2 = xeFiles.Elements("file").Where(x => x.Attribute("id").Value == "2").FirstOrDefault();
string file2Content = xeFile2.FirstNode.ToString();
 
Share this answer
 
v3
Comments
BillWoodruff 1-Jul-14 2:36am    
Thanks for your response, George; that is the obvious solution. My wish, in posting this question, is to find, if possible, a simpler way to do this perhaps using a feature of xml, or xslt, I am not familiar with.
George Jonsson 1-Jul-14 5:32am    
Sometimes the obvious answer is the right answer. :)
Well, both XDocument and XElement belongs to Linq technology.

You don't tell much about about your data structure. Without knowing that it is pretty difficult to start on the XSLT path in my experience.
George Jonsson 1-Jul-14 5:36am    
I can remove my answer if you think it is not adequate, so your question appears again in the unanswered list.
BillWoodruff 2-Jul-14 1:52am    
Hi George, please don't remove your answer; I just voted it #4, and I agree with you that there's nothing wrong with an "obvious" answer :) Your positive intention to be helpful is appreciated ! If there are no other replies in a few days, I will accept your answer. cheers, Bill
George Jonsson 2-Jul-14 4:15am    
Just for the fun of it I added an example as well. :)

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