Write an xml node in a specific position
-
Hi everybody, i'd like to know ('cause I'm on my first experience with c#) if it is possible with xmltextwriter, to write a new node in a specified position. Thank you
-
Hi everybody, i'd like to know ('cause I'm on my first experience with c#) if it is possible with xmltextwriter, to write a new node in a specified position. Thank you
Not with an
XmlTextWriter
, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use theXmlDocument
to load the XML and use one of the XPath methods likeSelectSingleNode
to find the node you want, then either prepend or append the node you want to add like so:class Program { static void Main(string\[\] args) { string xml = @"<?xml version=""1.0""?>
<root>
<child id=""one"">
<grandchild id=""a""/>
<grandchild id=""b""/>
</child>
<child id=""two"">
<grandchild id=""c""/>
<grandchild id=""d""/>
</child>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
if (node != null)
{
XmlElement elem = doc.CreateElement("grandchild");
elem.Attributes.Append(doc.CreateAttribute("id"));
elem.Attributes[0].Value = "e";
node.AppendChild(elem);
}
using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
{
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
}
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
-
Not with an
XmlTextWriter
, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use theXmlDocument
to load the XML and use one of the XPath methods likeSelectSingleNode
to find the node you want, then either prepend or append the node you want to add like so:class Program { static void Main(string\[\] args) { string xml = @"<?xml version=""1.0""?>
<root>
<child id=""one"">
<grandchild id=""a""/>
<grandchild id=""b""/>
</child>
<child id=""two"">
<grandchild id=""c""/>
<grandchild id=""d""/>
</child>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
if (node != null)
{
XmlElement elem = doc.CreateElement("grandchild");
elem.Attributes.Append(doc.CreateAttribute("id"));
elem.Attributes[0].Value = "e";
node.AppendChild(elem);
}
using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
{
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
}
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
Thank you so much...in fact I was reading msdn documentation for xmlDataDocument...now I'll go trying...thanks agains
-
Not with an
XmlTextWriter
, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use theXmlDocument
to load the XML and use one of the XPath methods likeSelectSingleNode
to find the node you want, then either prepend or append the node you want to add like so:class Program { static void Main(string\[\] args) { string xml = @"<?xml version=""1.0""?>
<root>
<child id=""one"">
<grandchild id=""a""/>
<grandchild id=""b""/>
</child>
<child id=""two"">
<grandchild id=""c""/>
<grandchild id=""d""/>
</child>
</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
if (node != null)
{
XmlElement elem = doc.CreateElement("grandchild");
elem.Attributes.Append(doc.CreateAttribute("id"));
elem.Attributes[0].Value = "e";
node.AppendChild(elem);
}
using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
{
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
}
}
}This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
well, now...i have the following structure of my xml file: - Now a I want to add dynamically new nodes under Variables, Parameters and BreakPoints, and for each subnode I have many properties to store, any suggestion?
-
well, now...i have the following structure of my xml file: - Now a I want to add dynamically new nodes under Variables, Parameters and BreakPoints, and for each subnode I have many properties to store, any suggestion?
-
You need to change < to < and > to >, or check "Ignore HTML tags in this message" when posting SGML-derivative code like HTML and XML. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]
-
Thank you so much...in fact I was reading msdn documentation for xmlDataDocument...now I'll go trying...thanks agains
Be aware that the
XmlDataDocument
is really designed for aDataSet
or similar schema. If you're XML fragment is more complex you should stick withXmlDocument
. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]