CopyPastehas never been so tasty!

Encode XML string in C#

by anonymous

  • 0
  • 0
  • 0
73 views

Encode:

 

Encoding is the process of putting a sequence of characters into a special format for transmission or storage purposes. In this article we learn that how we can encode an xml string.

 

Encode XML String:

 

You can easily encode xml string. First of all we will add the reference of xml name space and then we will use that xml namespace.

 

Using System.Xml;

 

And then we will create xml document in which we will add the xml element. And then add an element and strings in the elements. Then we will encode the string and create a document of xml string. 

 

To demonstrate make a console application and write the following code.

 

Code:

 

 

classEncode_XML_string

    {

staticvoid Main(string[] args)

        {

XmlDocument xml = newXmlDocument();

XmlElement root = xml.CreateElement("children");

xml.AppendChild(root);

 

XmlComment comment = xml.CreateComment("Children below...");

root.AppendChild(comment);

 

for (int i = 1; i < 10; i++)

            {

XmlElement child = xml.CreateElement("child");

child.SetAttribute("age", i.ToString());

child.SetAttribute("referenceNumber", "ref-" + i);

child.InnerText = "child & content #" + i;

root.AppendChild(child);

            }

 

string s = xml.OuterXml;

Console.WriteLine(s.ToString());

Console.ReadLine();

        }

 

    }

Add A Comment: