简体   繁体   English

什么是编写XML的最快方法

[英]what's the fastest way to write XML

I need create XML files frequently and I choose XmlWrite to do the job, I found it spent much time on things like WriteAttributeString ( I need write lots of attributes in some cases), my question is are there some better way to create xml files? 我需要经常创建XML文件,我选择XmlWrite来完成这项工作,我发现它花了很多时间在像WriteAttributeString这样的东西上(在某些情况下我需要写很多属性),我的问题是有没有更好的方法来创建xml文件? Thanks in advance. 提前致谢。

Fastest way that I know is two write the document structure as a plain string and parse it into an XDocument object: 我知道最快的方法是将文档结构写为普通字符串并将其解析为XDocument对象:

string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
    <Child>Content</Child>
</Root>";

XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);

Now you will have a structured and ready to use XDocument object where you can populate with your data. 现在,您将拥有一个结构化且随时可以使用的XDocument对象,您可以在其中填充数据。 Also, you can even parse a fully structured and populated XML as string and start from there. 此外,您甚至可以将完全结构化和填充的XML解析为字符串并从那里开始。 Also you can always use structured XElements like this: 您也可以使用这样的结构化XElements:

XElement doc =
  new XElement("Inventory",
    new XElement("Car", new XAttribute("ID", "1000"),
    new XElement("PetName", "Jimbo"),
    new XElement("Color", "Red"),
    new XElement("Make", "Ford")
  )
);
doc.Save("InventoryWithLINQ.xml");

Which will generate: 这会产生:

<Inventory>
  <Car ID="1000">
    <PetName>Jimbo</PetName>
    <Color>Red</Color>
    <Make>Ford</Make>
  </Car>
</Inventory>

Write it directly to a file via for example a FileStream (through manually created code). 通过例如FileStream (通过手动创建的代码)将其直接写入文件。 This can be made very fast , but also pretty hard to maintain. 这可以非常 ,但也很难维护。 As always, optimizations comes with a prize tag. 与往常一样,优化带有奖品标签。

Also, do not forget that " premature optimization is the root of all evil ". 另外,不要忘记“ 过早优化是万恶之源 ”。

XmlSerializer XmlSerializer的

You only have to define hierarchy of classes you want to serialize, that is all. 您只需要定义要序列化的类的层次结构,即全部。 Additionally you can control the schema through some attributes applied to your properties. 此外,您可以通过应用于属性的某些属性来控制架构。

使用匿名类型和序列化为XML是一种有趣的方法,如此处所述

How much is much time...is it 10 ms, 10 sec or 10 min...and how much of the whole process that writes an Xml is it? 多少时间......是10毫秒,10秒还是10分钟...写一个Xml的整个过程有多少呢?

Not saying that you shouldn't optimize but imo it's a matter of how much time do you want to spend optimizing that slight bit of a process. 并不是说你不应该优化,而是因为你需要花多少时间来优化那一点点的过程。 In the end the faster you wanna go, the more complex it will be to maintain in this case (personal opinion). 最后,你想要的速度越快,在这种情况下维持的就越复杂(个人意见)。

I personally like to use XmlDocument type. 我个人喜欢使用XmlDocument类型。 It's still a bit heavy when writing nodes but attributes are one-liner, and all in all way simpler that using Xmlwrite. 在编写节点时它仍然有点沉重,但是属性是单行的,并且使用Xmlwrite都更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM