1
Answer

adding elemts to a xml file (formating problem)

wertzui

wertzui

20y
1.9k
1
[code] XmlDocument doc = new XmlDocument(); doc.Load("data\\Fragen.xml"); XmlNodeList nodeList = doc.SelectNodes("//Fragen"); foreach (XmlNode node in nodeList) { if(Convert.ToInt32(node.Attributes["Id"].Value) > Id) Id = Convert.ToInt32(node.Attributes["Id"].Value); } Id = Id + 1; // II. Addition // 1. Create a new Book element. XmlElement newElem = doc.CreateElement("Fragen"); // Add the Id attribute. XmlAttribute newAttr = doc.CreateAttribute("Id"); newAttr.Value = Convert.ToString(Id); newElem.Attributes.Append(newAttr); // Create the child nodes. This code demonstrates various ways to add them. newElem.InnerXml = "" + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; newElem.InnerXml += " " + Environment.NewLine; XmlText txtNode = doc.CreateTextNode("FRAGE"); newElem.FirstChild.AppendChild(txtNode); newElem.AppendChild(doc.CreateWhitespace("\r\n")); // Linefeed newElem["Frage"].InnerText = txtFrage.Text; newElem["AntwortA"].InnerText = txtAntwortA.Text; newElem["AntwortB"].InnerText = txtAntwortB.Text; newElem["AntwortC"].InnerText = txtAntwortC.Text; newElem["AntwortD"].InnerText = txtAntwortD.Text; newElem["AntwortRichtig"].InnerText = AntwortRichtig; newElem["Geld1"].InnerText = Geld1; newElem["Geld2"].InnerText = Geld2; newElem["Geld3"].InnerText = Geld3; newElem["Geld4"].InnerText = Geld4; newElem["Geld5"].InnerText = Geld5; newElem["Geld6"].InnerText = Geld6; newElem["Geld7"].InnerText = Geld7; newElem["Geld8"].InnerText = Geld8; newElem["Geld9"].InnerText = Geld9; newElem["Geld10"].InnerText = Geld10; newElem["Geld11"].InnerText = Geld11; newElem["Geld12"].InnerText = Geld12; newElem["Geld13"].InnerText = Geld13; newElem["Geld14"].InnerText = Geld14; newElem["Geld15"].InnerText = Geld15; // 2. Add the new element to the end of the book list. doc.DocumentElement.AppendChild(newElem); // Display the output in Debug window. System.Diagnostics.Debug.Write("{0}\n", doc.OuterXml); // 2. Save the modified XML to a file in Unicode format. doc.PreserveWhitespace = true; XmlTextWriter wrtr = new XmlTextWriter("data\\Fragen.xml", Encoding.Unicode); doc.WriteTo(wrtr); wrtr.Close(); [/code] the problem is, that it writes the content, that was already in the xml file into one line, and not, like it was before in more lines. the new content is written into more than one line. thats good. but if i add more content then, the one, that was well formated before now is in a single line with everything else.
Answers (1)