Question

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!

Was it helpful?

Solution

What would be cleaner than WriteStartElement("Element.Attribute")? It describes exactly what you're doing - creating a new element, with that name.

If you definitely want to use XmlWriter, I'd stick with that approach. As Henrik says, however, LINQ to XML is generally a simpler way of creating XML in the first place:

XElement element = new XElement("Element",
    new XAttribute("Attribute1", "Value"),
    // This could contain nested elements instead of just a text node
    new XElement("Element.Attribute2", "Second value")
);

EDIT: Now you've updated the question, I still don't see why you want to use this "dot notation". Isn't it implicit in the hierarchy of the XML?

OTHER TIPS

There is no such thing as "dot notation". You seem to be referring to XAML. Beyond XAML, there is no such thing as "dot notation" in XML. That's why you're not finding any support for it - it doesn't exist.

Finally, I end up using this string.Format in this way. Here :

WriteXml(XmlWriter writer, Transform sender)
{
    string elementName = sender.GetType.Name;
    writer.WriteStartElement(elementName);   
    ............................
    ............................
    //and for each property inside a foreach
    writer.WriteStartElement(GetDotElement(elementName, propertyName));
}

private string GetDotElement(string elementName, string propertyName)
{
    return string.Format("{0}.{1}", elementName, propertyName);
}

Thanks for helping me!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top