Tuesday, May 08, 2007

Creating an XML Document using XMLDom and returning string as response...

Some answers are so straight forward but you just dont find them when you need them. I am happy that i am maintaining this blog and hopefully the next time when i need these answers i come back here.... :)

This is the XML i am trying to create -

//Create XML document
XmlDocument xmlDoc = new XmlDocument();

//Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("PropList");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);

//Create an XML element
XmlElement parentNode = xmlDoc.CreateElement("Prop");
//Create an XML attribute
XmlAttribute idAttribute = xmlDoc.CreateAttribute("PropID");
idAttribute.Value = i.ToString();
//Associate the attribute to the element


//Create an XML element
XmlElement newCityHocn = xmlDoc.CreateElement("CityHocn");
newCityHocn.InnerText = "MEMSG";
//Add this element as an child to Prop

//Create an XML element
XmlElement newSecFacilityID = xmlDoc.CreateElement("SecondaryFacilityID");
newSecFacilityID.InnerText = "1000121";
//Add this element as an child to Prop

// Save to the XML file

//return string

If you are creating a web service and you want to return string as an response to the web method then use xmlDoc.OuterXML instead of saving to a file. String response will help non .net platforms to consume from web service as well.

Others - http://www.mastercsharp.com/article.aspx?ArticleID=69&&TopicID=9

Thanks - Dipesh

No comments: