Adding a child node to an XML file using XDOCUMENT

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im trying to Append a Child element to my XML using XDocument. Im having difficulty with inserting

<pre class="prettyprint <DEVICE_INFORMATION>
<DEVICE_TYPE
DEVICE_TYPE="EmailAddress"/>
<DEVICE_VALUE>test@telemessage.com</DEVICE_VALUE>
</DEVICE_INFORMATION> [/code]
as an additional child section after last ...</DEVICE_VALUE>
Here is my code:

<pre class="prettyprint //Convert to XML :
XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XComment("SPRINT EMG Version 1.6 Output XML"),
new XElement("TELEMESSAGE",
new XElement("TELEMESSAGE_CONTENT",
new XElement("MESSAGE",
new XElement("MESSAGE_INFORMATION",
new XElement("SUBJECT", txtSubject.Text),
new XElement("TIME_STAMP", TimeStampSubmit)),
new XElement("USER_FROM",
new XElement("CIML",
new XElement("NAML",
new XElement("LOGIN_DETAILS",
new XElement("USER_NAME", UserName),
new XElement("PASSWORD", Password))))),
new XElement("MESSAGE_CONTENT",
new XElement("TEXT_MESSAGE",
new XElement("MESSAGE_INDEX", 0),
new XElement("TEXT", txtMessage.Text))),
new XElement("USER_TO",
new XElement("CIML",
//Insert Additional Child Nodes if Exist:
new XElement("DEVICE_INFORMATION",
new XElement("DEVICE_TYPE",
new XAttribute("DEVICE_TYPE", messagetype)),
new XElement("DEVICE_VALUE", txtPhone.Text))
// End of Additional Child Nodes
)))),
new XElement("VERSION", "1.6"))

);
//This Works:
xDocument.Add(new XComment("This is a comment."));
//This does not Work:
xDocument.Descendants("USER_TO").Last().Add(new XElement("DEVICE_INFORMATION"), new XElement("DEVICE_TYPE"), new XAttribute("DEVICE_TYPE", messagetype), new XElement("DEVICE_VALUE", txtPhone.Text));
[/code]
<br/>

<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt Correctly formatted, prior to append of child node:
<pre class="prettyprint <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--SPRINT EMG Version 1.6 Output XML-->
<TELEMESSAGE>
<TELEMESSAGE_CONTENT>
<MESSAGE>
<MESSAGE_INFORMATION>
<SUBJECT>NHCP Text Message from User: </SUBJECT>
<TIME_STAMP>20130109 09:18:54</TIME_STAMP>
</MESSAGE_INFORMATION>
<USER_FROM>
<CIML>
<NAML>
<LOGIN_DETAILS>
<USER_NAME>user</USER_NAME>
<PASSWORD>xyz</PASSWORD>
</LOGIN_DETAILS>
</NAML>
</CIML>
</USER_FROM>
<MESSAGE_CONTENT>
<TEXT_MESSAGE>
<MESSAGE_INDEX>0</MESSAGE_INDEX>
<TEXT>Enter Message Here...</TEXT>
</TEXT_MESSAGE>
</MESSAGE_CONTENT>
<USER_TO>
<CIML>
<DEVICE_INFORMATION>
<DEVICE_TYPE DEVICE_TYPE="SMS" />
<DEVICE_VALUE>Enter SMS Text Number</DEVICE_VALUE>
</DEVICE_INFORMATION>
</CIML>
</USER_TO>
</MESSAGE>
</TELEMESSAGE_CONTENT>
<VERSION>1.6</VERSION>
</TELEMESSAGE>
<!--This is a comment.-->[/code]
<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt Using
<span> the following C# XDocument command,
<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt
<pre class="prettyprint //This does not Work:
xDocument.Descendants("USER_TO").Last().Add(new XElement("DEVICE_INFORMATION"), new XElement("DEVICE_TYPE"), new XAttribute("DEVICE_TYPE", messagetype), new XElement("DEVICE_VALUE", txtPhone.Text)); [/code]
<span style="line-height:115%; font-family:Calibri,sans-serif; font-size:8pt This is now incorrectly formatted
<pre class="prettyprint <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--SPRINT EMG Version 1.6 Output XML-->
<TELEMESSAGE>
<TELEMESSAGE_CONTENT>
<MESSAGE>
<MESSAGE_INFORMATION>
<SUBJECT>NHCP Text Message from User: </SUBJECT>
<TIME_STAMP>20130109 09:17:26</TIME_STAMP>
</MESSAGE_INFORMATION>
<USER_FROM>
<CIML>
<NAML>
<LOGIN_DETAILS>
<USER_NAME>user</USER_NAME>
<PASSWORD>xyz</PASSWORD>
</LOGIN_DETAILS>
</NAML>
</CIML>
</USER_FROM>
<MESSAGE_CONTENT>
<TEXT_MESSAGE>
<MESSAGE_INDEX>0</MESSAGE_INDEX>
<TEXT>Enter Message Here...</TEXT>
</TEXT_MESSAGE>
</MESSAGE_CONTENT>
<USER_TO DEVICE_TYPE="SMS
<CIML>
<DEVICE_INFORMATION>
<DEVICE_TYPE DEVICE_TYPE="SMS" />
<DEVICE_VALUE>Enter SMS Text Number</DEVICE_VALUE>
</DEVICE_INFORMATION>
</CIML>
<DEVICE_INFORMATION />
<DEVICE_TYPE />
<DEVICE_VALUE>Enter SMS Text Number</DEVICE_VALUE>
</USER_TO>
</MESSAGE>
</TELEMESSAGE_CON TENT>
<VERSION>1.6</VERSION>
</TELEMESSAGE>
<!--This is a comment.-->[/code]
<br/>
<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt This is what the final result should look like:
<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-size:8pt
<pre class="prettyprint <?xml version="1.0" encoding="UTF-8" ?>
<TELEMESSAGE>
<TELEMESSAGE_CONTENT>
<MESSAGE>
<MESSAGE_INFORMATION>
<SUBJECT>enter your subject here</SUBJECT>
</MESSAGE_INFORMATION>
<USER_FROM>
<CIML>
<NAML>
<LOGIN_DETAILS>
<USER_NAME>enter your user name here</USER_NAME>
<PASSWORD>enter your password here</PASSWORD>
</LOGIN_DETAILS>
</NAML>
</CIML>
</USER_FROM>
<MESSAGE_CONTENT>
<TEXT_MESSAGE>
<MESSAGE_INDEX>0</MESSAGE_INDEX>
<TEXT>enter your text here</TEXT>
</TEXT_MESSAGE>
</MESSAGE_CONTENT>
<USER_TO>
<CIML>
<DEVICE_INFORMATION>
<DEVICE_TYPE DEVICE_TYPE="MobileNumber"/>
<DEVICE_VALUE>16173190666</DEVICE_VALUE>
</DEVICE_INFORMATION>
<DEVICE_INFORMATION>
<DEVICE_TYPE DEVICE_TYPE="EmailAddress"/>
<DEVICE_VALUE>test@telemessage.com</DEVICE_VALUE>
</DEVICE_INFORMATION>
</CIML>
</USER_TO>
</MESSAGE>
</TELEMESSAGE_CONTENT>
<VERSION>1.6</VERSION>
</TELEMESSAGE>[/code]
<br/>
<br/>

<br/>

View the full article
 
Back
Top