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.
-
do you know about XML Serialization? Instantiate a type, fill the properties, then serialize it to create an XML document representing the state of the object. – Cheeso Apr 18 '11 at 13:20
-
You are on the right track. XmlWriter is the fastest way to write XML. – KV Prajapati Apr 18 '11 at 13:21
-
Define "faster". Faster to write/debug or faster to execute? The answer will be different. – Seva Alekseyev Apr 18 '11 at 13:21
-
Could it be that it is actually not **WriteAttributeString** that is the bottleneck but the code used to supply data to it that is costly? For example a property doing too much work when running it's 'get'? – Jakob Möllås Apr 18 '11 at 13:31
6 Answers
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);
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");
Which will generate:
<Inventory>
<Car ID="1000">
<PetName>Jimbo</PetName>
<Color>Red</Color>
<Make>Ford</Make>
</Car>
</Inventory>

- 25,584
- 6
- 69
- 80
-
I don't find this efficient since you can't detect structure mistakes and typos until runtime. – Antoine Apr 18 '11 at 13:23
-
1
Write it directly to a file via for example a FileStream
(through manually created code). 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".

- 4,239
- 3
- 33
- 61
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.

- 2,571
- 17
- 23
Using anonymous types and serializing to XML is an interesting approach as mentioned here

- 1
- 1

- 15,800
- 1
- 42
- 60
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?
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).

- 2,026
- 1
- 16
- 26
I personally like to use XmlDocument
type. It's still a bit heavy when writing nodes but attributes are one-liner, and all in all way simpler that using Xmlwrite.

- 5,055
- 11
- 54
- 82