Hi I want to Add to an existing xmLDocument an Element . How?

  • Thread starter Thread starter ShayHk
  • Start date Start date
S

ShayHk

I have An XMLDocument and I want to add a new Element.
I want that the element will be shaped like this :

<event>
<X>1</X>
<Y>2</Y>
</event>

thanks
 
ok
I know that
the problem is the rest..
for example...


XmlElement element = doc.CreateElement( "event" );
XmlNode node1 = doc.CreateNode( "", "X", "" );
node1.InnerText = "1";
XmlNode node2 = doc.CreateNode( "", "Y", "" );
node2.InnerText = "2";
element.AppendChild(node1);
element.AppendChild(node2);



the problem is that it doesnt work
I think I dont build The node correctly
 
Done 2 ways (note latter includes xml marker, but this can be omitted
by setting the options):

XmlDocument doc = new XmlDocument();
XmlElement root = (XmlElement)
doc.AppendChild(doc.CreateElement("event"));
root.AppendChild(doc.CreateElement("X")).InnerText = "1";
root.AppendChild(doc.CreateElement("Y")).InnerText = "2";
Console.WriteLine(doc.OuterXml);

StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb)) {
writer.WriteStartElement("event");
writer.WriteElementString("X", "1");
writer.WriteElementString("Y", "2");
writer.WriteEndElement();
}
Console.WriteLine(sb);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top