I am writing an XML file in C# using XmlWriter. I am able to write Elements using WriteStartElement()
method and I can add attributes to this element using WriteAttributeString()
method. But if I want to add attribute using dot notation then how to do that?
<Element Attribute1="Value">
<Element.Attribute2> //How can i add attribute in this Notation.
//Add Value of Attribute2
</Element.Attribute2>
</Element>
I know that I can call WriteStartElement("Element.Attribute")
but I m looking for a cleaner approach. Is there any other way to do this?
Edit1:
I have an object(say obj) that is hierarchical(in the form of Tree), each node of this tree is having some properties which can further contain nodes. and I am saving this object in Xml. For that I am using XmlWriter. At runtime I iterate through the obj and read the type of node using GetType().Name and pass it to write an XmlNode and using GetType().GetProperties()
I get all properties of that node, then I use a foreach to go through the PropertyInfo array one by one and write the Name of PropertyInfo as attribute but in case when I am having a property that is assigned a node, I need to write the above Dot Notation for that. I am looking for a method where I will just pass my PropertyInfo and the object and it will write for me in desired format.
Thanks for any help!
Edit2:
For a particular node I have properties like Height and Width, like Children which is a Collection and resides implicitly in the hierarchy of Xml, and like Resources which will also be having some properties and each will be represented by nodes under the parent. But while saving will be written like:
<Parent.Resources>
<Resource1 ...../>
<Resource2 ...../>
</Parent.Resources>
Thanks for help!