XML message generation
-
Hello everyone, I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly. Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid. Any better approaches recommended? thanks in advance, George
-
Thanks natsuyaki, 1. I read some documents for XMLDocument and XMLElement, looks like we need to use DOM model, right? 2. I have not found some good samples for a beginner, I have XML knowledge before and just want to learn how to use the C# classes. Any referred samples? regards, George
-
Hello everyone, I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly. Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid. Any better approaches recommended? thanks in advance, George
You can use
XMLTextWriter
orXMLDocument
. XMLTextWriter will be faster.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
You can use
XMLTextWriter
orXMLDocument
. XMLTextWriter will be faster.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Good N a v a n e e t h, I have found a good sample from, http://www.c-sharpcorner.com/UploadFile/mahesh/ReadWriteXMLTutMellli2111282005041517AM/ReadWriteXMLTutMellli21.aspx[^] regards, George
-
Thanks natsuyaki, 1. I read some documents for XMLDocument and XMLElement, looks like we need to use DOM model, right? 2. I have not found some good samples for a beginner, I have XML knowledge before and just want to learn how to use the C# classes. Any referred samples? regards, George
George_George wrote:
looks like we need to use DOM model, right?
Yes.
XMLDocument
use DOM. When file is loaded, it loads the entire file into memory. So if the file is very large, this method is inefficient. CallingSave()
method on this class instance will save all the changes made to the instance.George_George wrote:
Any referred samples?
I think MSDN has enough documentation on using these classes. It's pretty easy.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
George_George wrote:
looks like we need to use DOM model, right?
Yes.
XMLDocument
use DOM. When file is loaded, it loads the entire file into memory. So if the file is very large, this method is inefficient. CallingSave()
method on this class instance will save all the changes made to the instance.George_George wrote:
Any referred samples?
I think MSDN has enough documentation on using these classes. It's pretty easy.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks N a v a n e e t h, XMLTextWriter will not use DOM model and only loads necessary nodes other than all nodes (which is done in DOM model)? regards, George
-
Thanks N a v a n e e t h, XMLTextWriter will not use DOM model and only loads necessary nodes other than all nodes (which is done in DOM model)? regards, George
George_George wrote:
XMLTextWriter will not use DOM model and only loads necessary nodes other than all nodes
XMLTextWriter
won't load any nodes. It is used to create XML documents. It has some methods which you can use to create nodes, attributes etc.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Hello everyone, I need to generate some XML message, but the message can not be reflected from members or properties of class instance directly. Currently, I am using StringBuilder to append strings into XML message manually (e.g. to generate the element's hierarchies and add attributes to element), I think this method is stupid. Any better approaches recommended? thanks in advance, George
-
George_George wrote:
XMLTextWriter will not use DOM model and only loads necessary nodes other than all nodes
XMLTextWriter
won't load any nodes. It is used to create XML documents. It has some methods which you can use to create nodes, attributes etc.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Sorry, N a v a n e e t h! My bad, I mean XMLTextReader, it will not load the entire tree as DOM, like XMLDocument, and it will only loads necessary nodes, right? regards, George
-
If your using .NET 3.5 Try to use XElement using System.Xml.Linq its pretty easy, fast and convinient when compared to DOM
Regards, Vythees Miles to go before sleep...
Thanks Vythees, I need to use .Net 2.0 in current project, do you have any suggestions? regards, George
-
Sorry, N a v a n e e t h! My bad, I mean XMLTextReader, it will not load the entire tree as DOM, like XMLDocument, and it will only loads necessary nodes, right? regards, George
Yes. It will not load the full file initially.
XMLDocument
class is also using a reader internally to fill the data.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Yes. It will not load the full file initially.
XMLDocument
class is also using a reader internally to fill the data.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks N a v a n e e t h, 1. So, can I understand that using XMLDocument has better performance compared with XMLTextReader, but bigger memory footprint. 2. XMLDocument can both read/write, but XMLTextReader can only read, and XMLTextWriter can only write? regards, George
-
Thanks N a v a n e e t h, 1. So, can I understand that using XMLDocument has better performance compared with XMLTextReader, but bigger memory footprint. 2. XMLDocument can both read/write, but XMLTextReader can only read, and XMLTextWriter can only write? regards, George
George_George wrote:
So, can I understand that using XMLDocument has better performance compared with XMLTextReader, but bigger memory footprint.
This depends on the XML file size. When you call Load() method in an
XMLDocument
classes instance, it reads all the nodes and forms a DOM and keeps in the memory. So when the file is huge, it will consume more memory. Performance is dependent of your scenario. If you need to read the XMLFile (not as DOM),XMLTextReader
will give good performance. For creating a new xml file,XMLTextWriter
will give good performance. Say, in a situation where you will add new nodes, change the attributes, and doing some XPath queries, then better choice would beXMLDocument
class.George_George wrote:
XMLDocument can both read/write, but XMLTextReader can only read, and XMLTextWriter can only write?
XMLDocument
class can do more than read/write. It supports XPath queries also.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
George_George wrote:
So, can I understand that using XMLDocument has better performance compared with XMLTextReader, but bigger memory footprint.
This depends on the XML file size. When you call Load() method in an
XMLDocument
classes instance, it reads all the nodes and forms a DOM and keeps in the memory. So when the file is huge, it will consume more memory. Performance is dependent of your scenario. If you need to read the XMLFile (not as DOM),XMLTextReader
will give good performance. For creating a new xml file,XMLTextWriter
will give good performance. Say, in a situation where you will add new nodes, change the attributes, and doing some XPath queries, then better choice would beXMLDocument
class.George_George wrote:
XMLDocument can both read/write, but XMLTextReader can only read, and XMLTextWriter can only write?
XMLDocument
class can do more than read/write. It supports XPath queries also.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Great N a v a n e e t h! If I only need to read XML documents (in a file) into memory and get some values for some elements, then I think using XMLDocument will always have better performance, since all nodes are in memory (compared with XMLTextReader, only parts of nodes are in memory). Why do you think it is not always true? regards, George
-
Great N a v a n e e t h! If I only need to read XML documents (in a file) into memory and get some values for some elements, then I think using XMLDocument will always have better performance, since all nodes are in memory (compared with XMLTextReader, only parts of nodes are in memory). Why do you think it is not always true? regards, George
George_George wrote:
If I only need to read XML documents (in a file) into memory and get some values for some elements, then I think using XMLDocument will always have better performance
If the file size is less, you won't find any performance differences. XMLDocument provides an easy way to load and edit data. You can go with any methods which really suits your scenario.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
George_George wrote:
If I only need to read XML documents (in a file) into memory and get some values for some elements, then I think using XMLDocument will always have better performance
If the file size is less, you won't find any performance differences. XMLDocument provides an easy way to load and edit data. You can go with any methods which really suits your scenario.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks N a v a n e e t h, If the size of file is big, using XMLDocument is of better performance? :-) regards, George
-
Thanks Vythees, I need to use .Net 2.0 in current project, do you have any suggestions? regards, George
-
Go for XMLDocument since its easy to maintain, futuristic, and supports XPath queries.
Regards, Vythees Miles to go before sleep...
Thanks Vythees, What do you think the pros and cons compared with XMLDocument and XMLTextWriter from functional and performance perspective? regards, George