views:

351

answers:

3

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!

+4  A: 

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?

Jon Skeet
While saving my Object into Xml I can not get "Element.Attribute2" using reflection, either i need to write manually everytime or I need to do something weird like string.Format("{0}.{1}", elementName, attributeName), that is not good at all
viky
Why is that "not good at all"? Again, it's saying exactly what you mean. I'm afraid it's still very unclear what you don't like about this... or why you want to use this notation in the first place. You mentioned reflection in the comment, but that's the first we've heard about it... what are you trying to do? Please give us the bigger picture.
Jon Skeet
I have updated my question. thanks!
viky
May be I am not better explaining the things in the way I should, thanks for helping me. I have again updated the question, trying to explain the points!!
viky
I'm sorry, your format is still not really clear to me... nor do I understand why you're so opposed to explicitly specifying the two parts of the element name when you need to. It seems a simple solution to me.
Jon Skeet
A: 

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.

John Saunders
It's also used in XMI version 1.
Pete Kirkham
Is it the same "notation"? What do the dots mean?
John Saunders
A: 

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!!

viky