XML创建(1)

解决问题帖子地址:点击打开链接

解决代码:

namespace ConsoleApp2
{
    using System.Xml;
    using System.Collections.Generic;
    class Program
    {
        static void Main(string[] args)
        {
            //初始化测试数据
            Folder folder = new Folder() { name = "MYNAEM" };
            IList<Placemark> placemarks = new List<Placemark>();
            placemarks.Add(new Placemark() { name = "20177", Point = new Point() { coordinates = "99.9298,26.51303" } });
            placemarks.Add(new Placemark() { name = "20178", Point = new Point() { coordinates = "99.9312,26.51302" } });
            placemarks.Add(new Placemark() { name = "20179", Point = new Point() { coordinates = "99.9358,26.51305" } });
            folder.Placemarks = placemarks;

            //构建XML
            XmlDocument xmlDocument = new XmlDocument();

            XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
            xmlDocument.AppendChild(xmlDeclaration);

            XmlNode folderNode = xmlDocument.CreateNode(XmlNodeType.Element, "Folder", null);

            XmlNode folderNameNode = xmlDocument.CreateNode(XmlNodeType.Element, "name", null);
            folderNameNode.InnerText = folder.name;
            folderNode.AppendChild(folderNameNode);

            foreach (var placemark in placemarks)
            {
                XmlNode placemarkNode = xmlDocument.CreateNode(XmlNodeType.Element, "Placemark", null);

                XmlNode placemarkNameNode = xmlDocument.CreateNode(XmlNodeType.Element, "name", null);
                placemarkNameNode.InnerText = placemark.name;

                XmlNode pointNode = xmlDocument.CreateNode(XmlNodeType.Element, "Point", null);

                XmlNode coordinatesNode = xmlDocument.CreateNode(XmlNodeType.Element, "coordinates", null);
                coordinatesNode.InnerText = placemark.Point.coordinates;
                pointNode.AppendChild(coordinatesNode);

                placemarkNode.AppendChild(placemarkNameNode);
                placemarkNode.AppendChild(pointNode);

                folderNode.AppendChild(placemarkNode);
            }
            xmlDocument.AppendChild(folderNode);
            xmlDocument.Save("D:\\my.xml");
        }
    }


    class Folder
    {
        public string name { get; set; }

        public IList<Placemark> Placemarks { get; set; }
    }

    class Placemark
    {
        public string name { get; set; }

        public Point Point { get; set; }
    }

    class Point
    {
        public string coordinates { get; set; }
    }
}

解决结果:

<?xml version="1.0" encoding="UTF-8"?>
<Folder>
  <name>MYNAEM</name>
  <Placemark>
    <name>20177</name>
    <Point>
      <coordinates>99.9298,26.51303</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>20178</name>
    <Point>
      <coordinates>99.9312,26.51302</coordinates>
    </Point>
  </Placemark>
  <Placemark>
    <name>20179</name>
    <Point>
      <coordinates>99.9358,26.51305</coordinates>
    </Point>
  </Placemark>
</Folder>


上一篇:冬季实战营学习报告


下一篇:正则表达式应用二