Search:

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;

client.Send(message);


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


Code:


XmlDocument xmlDoc = new XmlDocument();


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

xmlDoc.AppendChild(xmlDec);


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


//Adding info about the document type

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

xmlDoc.AppendChild(docType);


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

xmlAtt.Value = "value_for_xmlAtt";

root.SetAttributeNode(xmlAtt);

xmlDoc.AppendChild(root);


//Placing a comment in the Xml

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

root.AppendChild(xmlComment);


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

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

root.AppendChild(book1);


//Creating a Fragment

XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();

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

xmlDoc.DocumentElement.AppendChild(xmlDocFragment);


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

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

root.AppendChild(book2);


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


Output:



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.


Code:


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");


Output:


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.


Code:


XmlDocument xmlDoc = new XmlDocument();

//First, create the declaration


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

xmlDoc.AppendChild(declaration);

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

xmlDoc.AppendChild(books);


//Child 1 for BOOK

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

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

books.AppendChild(book1);


//Child 2 for BOOK

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

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

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

books.AppendChild(book2);


//Save the Xml

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


Output:


Saturday, May 8, 2010

Split function in C#

Here is a small post about using split function in c#. We are going to split a String by using delimiters. A delimiter will tell when to split the string. So here is a fragment of code to perform splitting.

String answer = "This$is|a|string";

char[] delimitator1 = new char[] { '$' };

char[] delimitator2 = new char[] { '|' };

string[] words = answer.Split(delimitator1);

Console.WriteLine("First word : " + words[0]);

//Note that now we split the second part of the string

String allRest = words[1];

string[] rest1 = allRest.Split(delimitator2);

Console.WriteLine("Second word : " + rest1[0]);

Console.WriteLine("Third word : " + rest1[1]);

Console.WriteLine("Fourth word : " + rest1[2]);


Output: