组合两个.xml文件(不合并!),并将组合文件解析为两个.xml流 [英] Combining two .xml files (not merging !), and parsing combined file into two .xml streams

查看:107
本文介绍了组合两个.xml文件(不合并!),并将组合文件解析为两个.xml流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个第三方组件,每个组件都有自己的内置序列化程序,可以将.xml写入流或文件。



我想保存一个包含两个.xml流的文件,但不合并它们......在组合它们的意义上,两个文件之间的差异丢失。



显然,我可以将一个文件追加到另一个文件中,并编写一些分隔符字符串,这样在解析时我可以区分这两个文件,但是,我怀疑使用LinqToXml有一个更简单的解决方案或者XSLT,或者其他......因此:这个问题。



在运行时我想读取合并文件,并快速创建两个.xml流每一个都可以喂到这两个组成部分,因此它们将重新组成。



我很欣赏这里关于战略的一般建议;已经有很长一段时间了,因为我已经搞砸了.xml。



谢谢,Bill

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

推荐答案

您可以尝试创建第三个结构,其他两个结构作为元素。

不完全确定这是你想要的。



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

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





这将保留现有结构,您可以使用例如将文件保存为一个或不同的部分 XElement





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

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();


这篇关于组合两个.xml文件(不合并!),并将组合文件解析为两个.xml流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆