Results 1 to 6 of 6

Thread: Xml: Append with \n

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Xml: Append with \n

    I have a code that looks like
    Code:
    [Test] public void Test() 
    		{
    			XmlDocument doc = new XmlDocument();
    			XmlElement e = doc.CreateElement("database");
    			e.SetAttribute("name", "Northwind");
    			e.SetAttribute("type", "MSSQL");
    			doc.PrependChild(e);
    
    			XmlElement ee = doc.CreateElement("table");
    			ee.SetAttribute("name", "Categories");
    			e.PrependChild(ee);
    
    			Console.WriteLine(e.OuterXml);
    		}
    When tested, it looks like
    Code:
    <database name="Northwind" type="MSSQL"><table name="Categories" /></database>
    What I really want is something like
    Code:
    <database name="Northwind" type="MSSQL">
        <table name="Categories" />
    </database>
    Any help?
    Last edited by nebulom; Oct 5th, 2006 at 07:36 PM.

  2. #2
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Xml: Append with \n

    That's a good question. I would also like to know the answer to this
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  3. #3
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Xml: Append with \n

    I believe that XmlDocument.Save() formats the XML

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Xml: Append with \n

    Yes. Make sure the PreserveWhiteSpace property is set to false and then call Save() to output the document with auto-indenting.

    Code:
    doc.Save(Console.Out);

  5. #5
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Xml: Append with \n

    Quote Originally Posted by axion_sa
    I believe that XmlDocument.Save() formats the XML
    thank you. You are correct Thank you for the help !
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Xml: Append with \n

    Thanks for the reply but I haven't got much time to test this one. But a truly thanks for you guys. I'm not with C# for now so I haven't tested it. Fortunately, Bram is happy (and so am I) that the .Save method formats the code. Thanks, this is resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width