XLinq: Beginners Walkthrough
Let’s play with Linq to XML (aka XLinq). XLinq is different from conventional .NET way to work with XML. Linq to XML is not the replacement it is smarter and superior way to work with XML with Language-INtegrated Query.
Let’s suppose you need to create XML content like
<employees>
<employee age="28">
<name>Wriju</name>
<email>wriju@abc.com</email>
</employee>
<employee age="18">
<name>Writam</name>
<email>writam@abc.com</email>
</employee>
</employees>
using the power of XLinq. If you notice my code here you will find the name of objects under XLinq library start with “X” instead of Xml.
Here we go
XElement _xml = new XElement("employees",
new XElement("employee",
new XElement("name", "Wriju"),
new XAttribute("age", "28"),
new XElement("email", "wriju@abc.com")
),
new XElement("employee",
new XElement("name", "Writam"),
new XAttribute("age", "18"),
new XElement("email", "writam@abc.com")
)
);
Console.WriteLine(_xml);
You can also save the content in proper XML format rather than just showing them in string.
_xml.Save(@"C:\MyXMLData.xml");
Now this XML has couple of things missing like <?xml version="1.0" encoding="utf-8"?>
To add this you need to create XDocument instead of XElement.
The code will look like
XDocument _doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Sample XML"),
new XElement("employees",
new XElement("employee",
new XElement("name", "Wriju"),
new XAttribute("age", "28"),
new XElement("email", "wriju@abc.com")
),
new XElement("employee",
new XElement("name", "Writam"),
new XAttribute("age", "18"),
new XElement("email", "writam@abc.com")
)
)
);
To just display the data use
Console.WriteLine(_doc);
To save use,
_doc.Save(@"C:\MyXML.xml");
Happy weekend!!!
Namoskar
Comments
Anonymous
March 12, 2008
Hi, I found _doc.Save does write the xml declaration but the console output is missing declaration. I tried _doc.ToString() and it didnt work either. Any idea? Thanks.Anonymous
March 13, 2008
@Frank, Please use XDeclaration() to manually create XML declaration and add it to your XElement. WrijuAnonymous
November 25, 2008
For anyone else still puzzled by this here is a workaround: StringWriter docStringWriter = new StringWriter(); XmlTextWriter docXmlWriter = new XmlTextWriter(docStringWriter); _xml.WriteTo(docXmlWriter); string doc = docStringWriter.ToString(); IMHO this is a bug, or else ToString should be renamed "ToStringExceptXDeclaration" or something to avoid confusion since WriteTo and Save both output the declaration.Anonymous
December 21, 2008
Hey Wriju, Thanks for nice article. And Thanks wittelw for workaround of xmldeclaration problem. But i struggled with this also actually his solution will create UTF-16 Xml Declartion [I guess which is default in .NET.], But i want UTF-8 Declaration for my XML Document. And i found the way which i would like to share with you guys[which can help others] Pls note that Here my XDocument name is xDocument which has LINQ generated XML //Load XML-//Create new System.xml.XmlDocument object XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xDocument.ToString()); //Load XML using XDcoument object //note that this xDocument dosen't contain XmlDeclaraion //Create an XML declaration. XmlDeclaration xmlDeclaration; xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null); //PROVIDE your Encoding here //Add the new node-XML Declaration at the root to the document. XmlElement root = xmlDocument.DocumentElement; //gets root of document xmlDocument.InsertBefore(xmlDeclaration, root); //gets the full XML[xml with xml declaration in string] string xmlDocumentWithDeclaration = xmlDocument.InnerXml; now you can look at xmlDocumentWithDeclaration string which has XML with XmlDeclarion...hope it will help someone...Enjoy!!!! Regards, KP "Passionate .NET Developer"Anonymous
October 05, 2009
Why not just inherit from XDocument and override tostring? Then you retain the XDocument methods and fix this issue.Anonymous
February 19, 2010
Honestly... this is all well and good but did no one simply try: string rawdata = _doc.Declaration.ToString() + _doc.ToString(); I suppose you could use the StringBuilder class instead but otherwise, what's wrong with that?