Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<TOA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MDF_TOA_ID>5</MDF_TOA_ID>
<TOA_ID>12345</TOA_ID>
<Status>InProgress</Status>
<Reason>Cancelling TOA</Reason>
<MessageMode>Cancel</MessageMode>
</TOA>



i want to create this message in c#.help me to do

right now i m doing this

XML
XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
        doc.AppendChild(dec);

        XmlElement rootnode = doc.CreateElement("TOA");       

        XmlElement childOne = doc.CreateElement("MDF_TOA_ID");
        childOne.InnerText = Convert.ToString(MdfToaID);
        rootnode.AppendChild(childOne);

        XmlElement childTwo = doc.CreateElement("TOA_ID");
        childTwo.InnerText = Convert.ToString(toa_id);
        rootnode.AppendChild(childTwo);

        XmlElement childThree = doc.CreateElement("Status");
        childThree.InnerText = Convert.ToString("Cancel");
        rootnode.AppendChild(childThree);

        XmlElement childFour = doc.CreateElement("Reason");
        childFour.InnerText = Convert.ToString(reason);
        rootnode.AppendChild(childFour);

        XmlElement childFive = doc.CreateElement("MessageMode");
        childFive.InnerText = Convert.ToString("Cancel");
        rootnode.AppendChild(childFive);

        rootnode.AppendChild(rootnode);



        doc.AppendChild(rootnode);



but it is not my desire result.


please help me
Posted

1 solution

Try removing:
rootnode.AppendChild(rootnode);


The root node cannot be a child of itself.


Then add the following two lines:
doc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
doc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");


Add this after doc.AppendChild(rootnode);
 
Share this answer
 
v2
Comments
prince_rumeel 12-Jun-13 3:06am    
yeah i got it..

right now i m getting this out put



<toa><mdf_toa_id>2<toa_id>12345<status>Cancel<reason>Testing Cancelation message<messagemode>Cancel





but i need to get this one



<toa xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<mdf_toa_id>5
<toa_id>12345
<status>InProgress
<reason>Cancelling TOA
<messagemode>Cancel



mean to say i m not getting this line


<toa xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

in the start
BotCar 12-Jun-13 3:40am    
I'm not sure I understand what you mean. What line are you not getting? Is it this line: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema";

If so, try adding the following lines to your code (after doc.AppendChild(rootnode);): doc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");



If this is not what you mean, please explain what the problem is.
prince_rumeel 12-Jun-13 4:37am    
thx ii solve it :) with ur help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900