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);
xmlDoc.AppendChild(rootNode);
//
//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
parentNode.SetAttributeNode(idAttribute);
xmlDoc.DocumentElement.PrependChild(parentNode);
//
//Create an XML element
XmlElement newCityHocn = xmlDoc.CreateElement("CityHocn");
newCityHocn.InnerText = "MEMSG";
//Add this element as an child to Prop
parentNode.AppendChild(newCityHocn);
//
//Create an XML element
XmlElement newSecFacilityID = xmlDoc.CreateElement("SecondaryFacilityID");
newSecFacilityID.InnerText = "1000121";
//Add this element as an child to Prop
parentNode.AppendChild(newSecFacilityID);
//
// Save to the XML file
//xmlDoc.Save(@"D:\test.xml");
//return string
xmlDoc.OuterXML;
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:
Post a Comment