all groups > dotnet xml > march 2007 >
You're in the

dotnet xml

group:

How to use xmlwriter to add an attribute to any element?


How to use xmlwriter to add an attribute to any element? darrel
3/29/2007 11:45:14 AM
dotnet xml:
I want to make my XML as such:

<parentItem>
<childItem attribute="myAttribute">myContent</childItem>
</parentItem>

I thought I could just do this:

objXMLWriter.WriteElementString("childItem", "myContent")
objXMLWriter.WriteAttributeString("attribute", "myAttribute")

But...I get an error. And I found this that explains why:

http://weblogs.asp.net/sonukapoor/pages/204572.aspx

However, it only explains why it happens, not how to actually do what I
want. From what I can tell, WriteAttributeString can ONLY be applied to a
WriteStartElement. Not a WriteElementString.

So, as such, how does one do the above example where I want to add both an
attribute and content to an element?

-Darrel

Re: How to use xmlwriter to add an attribute to any element? darrel
3/29/2007 1:55:33 PM
[quoted text, click to view]

That seems so obvious once you see it. Thanks!

-Darrel

Re: How to use xmlwriter to add an attribute to any element? Martin Honnen
3/29/2007 7:04:05 PM
[quoted text, click to view]

xmlWriter.WriteStartElement("parentItem");
xmlWriter.WriteStartElement("childItem");
xmlWriter.WriteAttributeString("attribute", "myAttribute");
xmlWriter.WriteString("myContent");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button