什么是写XML的最快方法 [英] what's the fastest way to write XML

查看:117
本文介绍了什么是写XML的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要频繁地创建XML文件,我选择XmlWrite做的工作,我发现它花了很多时间之类的东西WriteAttributeString(我需要编写大量的属性在某些情况下),我的问题是有没有一些更好的方法来创建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? Thanks in advance.

推荐答案

最快的方式,我知道的是两个写文档结构作为一个纯字符串,解析成一个XDocument对象:

Fastest way that I know is two write the document structure as a plain string and parse it into an XDocument object:

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

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

现在你将有一个结构化的,并准备使用XDocument对象在这里您可以与您的数据填充。此外,你甚至可以解析完全结构化和填充XML作为字符串,并从那里开始。您也可以随时使用结构化XElements是这样的:

Now you will have a structured and ready to use XDocument object where you can populate with your data. Also, you can even parse a fully structured and populated XML as string and start from there. Also you can always use structured XElements like this:

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

这将产生:

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

这篇关于什么是写XML的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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