首页 > 代码库 > Read and Create XML

Read and Create XML

Xml makes it easy to read and write data in any application. That‘s enables data exchange between XML quickly became the only common language.

This chapter mainly on using Xml with C#,for reading and creating XML.

1.XML basic format

技术分享
<?xml version="1.0" encoding="utf-8"?>
<LanguagePluraRules>
  <Language Name="Arabic" Code="ar-SA">
    <Type Name="cardinal">
      <Range>
        <Category>zero</Category>
        <Examples>0</Examples>
      </Range>
      <Range>
        <Category>one</Category>
        <Examples>1</Examples>
      </Range>
      <Range>
        <Category>two</Category>
        <Examples>2</Examples>
      </Range>
      <Range>
        <Category>few</Category>
        <Examples>3</Examples>
      </Range>
      <Range>
        <Category>many</Category>
        <Examples>11</Examples>
      </Range>
      <Range>
        <Category>other</Category>
        <Examples>100</Examples>
      </Range>
    </Type>
  </Language>
</LanguagePluraRules>
View Code

2.Create XML

  (1). Create Xml string header

技术分享

  (2).  Create the single node and set attribute

技术分享

  (3). create same code and set innerText value

 技术分享

   (4). Append child 

技术分享

  (5). Code

技术分享
 public static XmlDocument CreateXML()
        {

            var list = new List<Model>
            {
                new Model { Category = "zero", Examples = "0" },
                new Model { Category = "one", Examples = "1" },
                new Model { Category = "two", Examples = "2" },
                new Model { Category = "few", Examples = "3" },
                new Model { Category = "many", Examples = "11" },
                new Model { Category = "other", Examples = "100" },
            };
            //Create XML String Header
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmlDoc.AppendChild(xmldecl);
           
            //xmlDoc.AppendChild(xeRoot);
            XmlElement LanguagePluraRules = xmlDoc.CreateElement("LanguagePluraRules");
            XmlElement Language = xmlDoc.CreateElement("Language");
            XmlElement Type = xmlDoc.CreateElement("Type");
            Language.SetAttribute("Name", "Arabic");
            Language.SetAttribute("Code", "ar-SA");
            Type.SetAttribute("Name", "cardinal");


            //set node value
            foreach (var vm in list)
            {
                XmlElement Range = xmlDoc.CreateElement("Range");
                XmlElement Category = xmlDoc.CreateElement("Category");
                XmlElement Examples = xmlDoc.CreateElement("Examples");
                Category.InnerText=vm.Category;
                Examples.InnerText = vm.Examples;
                Range.AppendChild(Category);
                Range.AppendChild(Examples);
                Type.AppendChild(Range);
            }


            LanguagePluraRules.AppendChild(Language);
            Language.AppendChild(Type);
            xmlDoc.AppendChild(LanguagePluraRules);
            xmlDoc.Save(@"..\..\Test.xml");
            return xmlDoc;
        }
View Code

  (6). Rendering 

技术分享

  (7). Focus

if you want create more same node in the xml . Whenever you create node you need to use CreateElement("Node_Name")  method .

技术分享
  //set node value
            foreach (var vm in list)
            {
                XmlElement Range = xmlDoc.CreateElement("Range");
                XmlElement Category = xmlDoc.CreateElement("Category");
                XmlElement Examples = xmlDoc.CreateElement("Examples");
                Category.InnerText=vm.Category;
                Examples.InnerText = vm.Examples;
                Range.AppendChild(Category);
                Range.AppendChild(Examples);
                Type.AppendChild(Range);
            }
View Code

 

3.Read XML

  (1). using system Xml

using System.Xml;

  (2). load the Xml select single node . According single node to get all children of the root node. And then read the children node value use the foreach method.

技术分享
public static void ReadXml()
        {
            //set xmldocument
            XmlDocument doc = new XmlDocument();
            //load xml
            doc.Load(@"..\..\LanguagePluralRules.xml");
            //select Single Node
            XmlNode xn = doc.SelectSingleNode("LanguagePluraRules");
            //get all children of the root node
            XmlNodeList xnlanguage = xn.ChildNodes;
            //read the children node 
            foreach (XmlNode xnL in xnlanguage)
            {
                XmlElement xe = (XmlElement)xnL;
                string LanguageName = xe.GetAttribute("Name").ToString();
                string LanguageCode = xe.GetAttribute("Code").ToString();
                XmlNodeList xnType = xe.ChildNodes;
                foreach (XmlNode xnT in xnType)
                {
                    XmlElement xnt = (XmlElement)xnT;
                    XmlNodeList xnRange = xnt.ChildNodes;
                    foreach (XmlNode xnr in xnRange)
                    {
                        XmlElement xnC = (XmlElement)xnr;
                        XmlNodeList xnCategory = xnC.ChildNodes;
                        string Categoryes = xnCategory.Item(0).InnerText;
                        int Examples = Convert.ToInt16(xnCategory.Item(1).InnerText);
                    }
                }
            }
            return;
        }
View Code

Read and Create XML