怎么组成一个XML文档

如何组成一个XML文档

//例如这个list中有三个 xml路径(可能有多个),C#如何根据路径组成一个xml呢?
//不使用linq to xml
 List<string> list=new List<string>(){"/ap/a","/ap/b","ap/c/d/f"};

//结果应该是这样

<ap>
   <a></a>
   <b></b>
   <c>
      <d>
         <f></f>
      </d>
  </c>
</ap>

------解决方案--------------------
别偷懒,用xmldocument自己组织
------解决方案--------------------
引用:
别偷懒,用xmldocument自己组织


+1

参考:http://bbs.****.net/topics/390625087
------解决方案--------------------
如果路径开个都是一样,丑陋的实现了个,自己去优化下吧
static void Main()
        {
            int i,j;
            XmlNode parent = null;
            string xPath;
            List<string> list = new List<string>() { "/ap/a", "/ap/b", "ap/c/d/f" };
            XmlDocument document = new XmlDocument();
            XmlNode xmlDeclaration = document.CreateXmlDeclaration("1.0", "UTF-8", null);
            document.AppendChild(xmlDeclaration);
         
            foreach (string str in list)
            {
                xPath = string.Empty;
                string[] nodes = str.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                                
                if (document.DocumentElement != null)
                {
                    parent = null;
                    for (i = 0; i < nodes.Length; i++)
                    {
                        xPath += "/" + nodes[i];
                        //查找节点是否已存在
                        XmlNode xmlNode = document.SelectSingleNode(xPath);
                        if (xmlNode == null)
                        {
                            XmlNode newNode = document.CreateNode(XmlNodeType.Element, nodes[i], "");
                            parent.AppendChild(newNode);
                            parent = newNode;
                        }
                        else
                        {
                            parent = xmlNode;
                        }