(my other elements will go here as below)
My problem is getting the element to output as above, below is an example of an xml output of mine that is working fine, i just need to add the element as above?
(my above sample here)
XElement xml = new XElement("Persons", new XElement("Person",
new XElement("Server", Server.Text),
new XElement("User", Username.Text),
new XElement("Password", Password.Text),
new XElement("Recipient1", Recipient.Text),
new XElement("Recipient2", Recipient2.Text),
new XElement("Recipient3", Recipient3.Text),
new XElement("Subject", Subject.Text),
new XElement("Message", Message.Text),
new XElement("Ping", Ping.Text)));
(and ends here)
xml.Save("c:\\XMLFile.xml");

VulpesRank #55 · 29.2K reputationPosted Feb 16, 2012, 3:01 AM EST
XElement rpc = new XElement("rpc",
new XAttribute("module", "SMS"),
new XAttribute("method", "Send_SMS"),
new XAttribute("version", "1.0"));
rpc.Add(xml); // xml is your existing XElement
rpc.Save("c:\\XMLFile.xml");
Mahesh ChandRank #1 · 393.3K reputationPosted Feb 16, 2012, 2:24 AM EST
You can also create direct XML using XmlDocument.LoadXml as you can see below. You can also create an XmlAttribute using XmlDocument.CreateAttribute (see below).
using System; using System.Xml; class test { public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml( " text1 text2 " ); // Get the attributes of node "child2" XmlAttributeCollection ac = doc.DocumentElement.ChildNodes[1].Attributes; // Print out the number of attributes and their names. Console.WriteLine( "Number of Attributes: "+ac.Count ); for( int i = 0; i ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" ); // Get the 'attr1' from child1. XmlAttribute attr = doc.DocumentElement.ChildNodes[0].Attributes[0]; // Add this attribute to the attributecollection "ac". ac.SetNamedItem( attr ); // 'attr1' will be removed from 'child1' and added to 'child2'. // Print out the number of attributes and their names. Console.WriteLine( "Number of Attributes: "+ac.Count ); for( int i = 0; i ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" ); // Create a new attribute and add to the collection. XmlAttribute attr2 = doc.CreateAttribute( "attr4" ); attr2.Value = "val4"; ac.SetNamedItem( attr2 ); // Print out the number of attributes and their names. Console.WriteLine( "Number of Attributes: "+ac.Count ); for( int i = 0; i ". Attribute Name: '" +ac[i].Name+ "' Attribute Value: '"+ ac[i].Value +"'" ); } }mike DelvottiRank #7,298 · 244 reputationPosted Feb 16, 2012, 3:42 AM EST
System.Xml.XmlAttribute' does not contain a constructor that takes '2' arguments
I have no doubt that I have placed it wrong somewhere though, have i done it correct as below:
XElement rpc = new XElement("rpc",
new XmlAttribute("module", "SMS"),
new XmlAttribute("method", "Send_SMS"),
new XmlAttribute("version", "1.0"));
XElement xml = new XElement("Persons", new XElement("Person",
new XElement("Server", Server.Text),
new XElement("User", Username.Text),
new XElement("Password", Password.Text),
new XElement("Recipient1", Recipient.Text),
new XElement("Recipient2", Recipient2.Text),
new XElement("Recipient3", Recipient3.Text),
new XElement("Subject", Subject.Text),
new XElement("Message", Message.Text),
new XElement("Ping", Ping.Text)));
rpc.Add(xml);
rpc.Save("c:\\XMLFile.xml");
VulpesRank #55 · 29.2K reputationPosted Feb 16, 2012, 5:36 AM EST
Otherwise it should work fine.
mike DelvottiRank #7,298 · 244 reputationPosted Feb 16, 2012, 5:43 AM EST