Wednesday, May 5, 2010

Add Namespace in XDocument

//Create XmlDocumetn Object
XDocument xmlDoc;
//Create XmlElement Object
XElement currentElement;

//NameSpace Constance
XNamespace xmlns = XNamespace.Get("http://www.fi2992.se/bygginfo_001");
XNamespace xmlns1 = XNamespace.Get("http://www.fi2002.se/fi2base");


//Create Xml Document
xmlDoc = new XDocument();
///Add Comment
xmlDoc.Add(new XComment("Header Area"));
//Add Root Element
XElement xRoot = new XElement("FI2002");
xmlDoc.Add(xRoot);

//Point the Cursur to Current Element
currentElement = xmlDoc.Descendants("FI2002").FirstOrDefault();
//To Write the Header Element

currentElement.Add( new XAttribute(XNamespace.Xmlns + "x", xmlns), new XAttribute(XNamespace.Xmlns + "fib", xmlns1));

Then Save the Xml Document.

The Xml Document Looks like below.


xmlns:x="http://www.fi2992.se/bygginfo_001" xmlns:fib="http://www.fi2002.se/fi2bas"

No comments:

Post a Comment