Custom Search

Saturday, July 24, 2010

Converting XML to String

In this short post I will show you how to convert XML to String, o viceversa, in order for example, to send it via Tcp/Ip.

In this example, a Client is going to genereta a XML Document and convert it to string.

The Server side must recive the string and generate the XML.

(Here you will not see how the communication is done, for example the convertion between string and stream)

Client Code:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(@"C:\XML\" + test + ".xml");

//Getting the Xml as String

String message = xmlDoc.InnerXml;


Server Code:

String innerXml = response;

XmlDocument xmlServer = new XmlDocument();

//Set the string to the Xml created above

xmlServer.InnerXml = innerXml;

Tuesday, July 20, 2010

More XML options in C#

Continuing the xml posts, in this oportunity I will show you more options to create your XmlDocument.

Here we will see:

- Adding comment to the xml document (XmlComment)

- Adding fragment (XmlDocumentFragment)

- Creation of attributes (XmlAttribute)

- Adding information about the document Type (XmlDocumentType)

- And also, declarations and elements


XmlDocument xmlDoc = new XmlDocument();

XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);


XmlElement root = xmlDoc.CreateElement("root-element");

//Adding info about the document type

XmlDocumentType docType = xmlDoc.CreateDocumentType("BOOKS", null, null, null);


XmlAttribute xmlAtt = xmlDoc.CreateAttribute("attribute");

xmlAtt.Value = "value_for_xmlAtt";



//Placing a comment in the Xml

XmlComment xmlComment = xmlDoc.CreateComment("First Book information");


XmlElement book1 = xmlDoc.CreateElement("book-1");

book1.SetAttribute("name", "Harry Potter");


//Creating a Fragment

XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();

xmlDocFragment.InnerText = "---------Inner text-------";


XmlElement book2 = xmlDoc.CreateElement("book-2");

book2.SetAttribute("name", "Robles");


xmlDoc.Save(@"C:\XML\" + "xmlTest.xml");


Saturday, July 17, 2010

Xml document edition

In the previous post I showed you how to create a XmlDocument following some easy steps. The idea now is to read the created xml and find elements by a specific tag. Then we are going to change that value and save the edited xml.


For the first book element, we are going to add inner text:

XmlDocument xmlDoc = new XmlDocument();

//Load the Xml created previously

xmlDoc.Load(@"C:\XML\" + "xmlTest.xml");

XmlNodeList books;

books = xmlDoc.GetElementsByTagName("book-1");

//Since there is only one element with this tag, the loop is not necessary.

foreach (XmlNode node in books)


node.InnerText = "Great Book";


For the second element, we are changing the attributes values:

books = xmlDoc.GetElementsByTagName("book-2");

foreach (XmlNode node in books)


node.Attributes[0].Value = "Time";


xmlDoc.Save(@"C:\XML\" + "xmlTest.xml");


Friday, July 16, 2010

Xml Document creation using DOM

In this article I´ll show you how to create a .xml document using proper tags and definitions. To do this you need to import the System.Xml library.

The idea is simple and the best way to learn this is by a small example.

Here I´ll create a XmlDocument with a couple of child elements.


XmlDocument xmlDoc = new XmlDocument();

//First, create the declaration

XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);


XmlElement books = xmlDoc.CreateElement("BOOK");


//Child 1 for BOOK

XmlElement book1 = xmlDoc.CreateElement("book-1");

book1.SetAttribute("name", "Harry Potter");


//Child 2 for BOOK

XmlElement book2 = xmlDoc.CreateElement("book-2");

book2.SetAttribute("name", "People");

book2.SetAttribute("type", "magazine");


//Save the Xml

xmlDoc.Save(@"C:\XML\" + "xmlTest.xml");