Create A XML doc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scriptick
    New Member
    • Apr 2013
    • 15

    #1

    Create A XML doc

    I use this code -
    Code:
     XmlWriterSettings settings = new
    XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = (" " );
    settings.CloseOutput = true;
    settings.OmitXmlDeclaration = true;
    using (XmlWriter writer = XmlWriter.Create
    ("books.xml" , settings))
    {
    writer.WriteStartElement( "book" );
    writer.WriteElementString( "title" , book.Title);
    writer.WriteElementString( "author" ,
    book.Author.Name);
    writer.WriteElementString( "publisher" ,
    book.Publisher);
    writer.WriteElementString( "price" ,
    book.Price.ToString());
    writer.WriteEndElement();
    writer.Flush();
    }
    
    output in pavel.xml is 
    
    <book>
    .....
    ........
    </book>
    
    but I wanna output 
    
    <book Key ="A">
    ....
    ...
    ...
    </book>
    Just this. nothing else.

    How to do this?
    Last edited by Rabbit; Oct 6 '13, 03:42 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    Those extra pieces of information found in the opening tags of XML are called attributes. You need to look into the WriteAttributeS tring method of XmlWriter.

    After your line 10 you would need an extra line of code like this:

    Code:
    writer.WriteStartElement( "book" );
    writer.WriteAttributeString("Key", "A");
    Check out the documentation here (MSDN documentation for WriteAttributeS tring)

    Comment

    • scriptick
      New Member
      • Apr 2013
      • 15

      #3
      Joseph Martell thanks a lot!
      this is that what I am looking for!

      Comment

      Working...