Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
darrel - 29 Mar 2007 17:45 GMT
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
Martin Honnen - 29 Mar 2007 18:04 GMT
> I want to make my XML as such:
>
> <parentItem>
>    <childItem attribute="myAttribute">myContent</childItem>
> </parentItem>

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

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

darrel - 29 Mar 2007 19:55 GMT
>         xmlWriter.WriteStartElement("parentItem");
>         xmlWriter.WriteStartElement("childItem");
>         xmlWriter.WriteAttributeString("attribute", "myAttribute");
>         xmlWriter.WriteString("myContent");
>         xmlWriter.WriteEndElement();
>         xmlWriter.WriteEndElement();

That seems so obvious once you see it. Thanks!

-Darrel

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.