XML indented

How to use the XML functions and get the text indented

Page 1 of 1

6 Replies - 4776 Views - Last Post: 24 November 2008 - 11:41 PM Rate Topic: -----

#1 maffelu   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 190
  • Joined: 21-August 08

XML indented

Posted 24 November 2008 - 12:05 PM

Elo

I'm trying to learn how to use the XML functions in C#, and they are great =).
However, when I append elements to an existing .xml file, all the elements I put in there get added in one long line. That's no good...

So, this is my code for saving data to xml:
public void appendToXMLFile(string name, string amount, string recurring)
		{
			XmlDocument doc = new XmlDocument();
			doc.Load("mySave.txt");
			XmlElement newCategory = doc.CreateElement("cost");
			newCategory.InnerText = name;

			XmlElement nameCt = doc.CreateElement("name");
			nameCt.InnerText = name;
			newCategory.AppendChild(nameCt);

			XmlElement amountCt = doc.CreateElement("amount");
			amountCt.InnerText = amount;
			newCategory.AppendChild(amountCt);

			XmlElement recCt = doc.CreateElement("recurring");
			recCt.InnerText = recurring;
			newCategory.AppendChild(recCt);
			
			
			

			doc.DocumentElement.InsertAfter(newCategory, doc.DocumentElement.LastChild);

			XmlTextWriter writer = new XmlTextWriter("mySave.txt", null);
			writer.Formatting = Formatting.Indented;

			doc.Save(writer);

			
		}


But when I add this, this is how it looks:


<?xml version="1.0"?>
<november>
<cost>
<name>Rent</name>
<amount>5045</amount>
<recurring>Yes</recurring>
</cost>
<cost>
<name>ComHem</name>
<amount>1160</amount>
<recurring>No</recurring>
</cost>
<cost>Godis<name>Godis</name><amount>2300</amount><recurring>No</recurring></cost>
</november>

(The other elements are placed there by other means. It's the bold line that I just entered, and look how terrible it's entered...)

Anyway to get around this?

Is This A Good Question/Topic? 0
  • +

Replies To: XML indented

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: XML indented

Posted 24 November 2008 - 12:29 PM

XmlWriterSettings, if you're using an XmlWriter.
Was This Post Helpful? 0
  • +
  • -

#3 maffelu   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: XML indented

Posted 24 November 2008 - 01:10 PM

But I'm not using that. I've looked at XMLWriter and it's properties, but that's not what I'm using. Any otherway?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: XML indented

Posted 24 November 2008 - 01:55 PM

It's a code/XML validity problem. Get rid of the line I commented out below.
XmlDocument doc = new XmlDocument();
doc.Load("mySave.txt");
XmlElement newCategory = doc.CreateElement("cost");
//newCategory.InnerText = name;

This post has been edited by JackOfAllTrades: 24 November 2008 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: XML indented

Posted 24 November 2008 - 01:57 PM

It should work. You might also try adding
writer.Indentation = 3;



Maybe even mess with IndentChar as well.
Was This Post Helpful? 0
  • +
  • -

#6 maffelu   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: XML indented

Posted 24 November 2008 - 11:27 PM

But how can I skipp the innertext, taht's where I enter the data`?
Was This Post Helpful? 0
  • +
  • -

#7 maffelu   User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 190
  • Joined: 21-August 08

Re: XML indented

Posted 24 November 2008 - 11:41 PM

No wait! I see now what you mean. Awesome, this works. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1