XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
            doc.AppendChild(dec);            XmlElement t = doc.CreateElement("T");
            t.SetAttribute("xmlns:v","urn:schemas-microsoft-com:vml");
            doc.AppendChild(t);            int gridWidth = 350;
            int halfWidth = gridWidth / 2;
            int x, y;
            x = y = halfWidth;
            int w, h;
            w = h = halfWidth;
            string attribute;
            XmlElement rect = doc.CreateElement("v:rect");
            attribute="left:"+x+";top:"+y+";width:"+w+";height:"+h;
            rect.SetAttribute("style", attribute);
            rect.SetAttribute("filled", "false");
            //rect.SetAttribute("stroked", "false");
            t.AppendChild(rect);
            XmlElement txt = doc.CreateElement("v:textbox");
            attribute="color:Blue; font-size:9";
            txt.SetAttribute("style", attribute);
            txt.InnerText = "28.4";
            rect.AppendChild(txt);
            Console.Write(doc.OuterXml);
            Console.ReadKey();为什么生成是这样<?xml version="1.0" encoding="GB2312"?>
<T xmlns:v="urn:schemas-microsoft-com:vml">
  <rect style="left:175;top:175;width:175;height:175" filled="false" stroked="false">
    <textbox style="color:Blue; font-size:9">28.4</textbox>
  </rect>
</T>而不是这样<?xml version="1.0" encoding="GB2312"?>
<T xmlns:v="urn:schemas-microsoft-com:vml">
  <v:rect style="left:175;top:175;width:175;height:175" filled="false" stroked="false">
    <v:textbox style="color:Blue; font-size:9">28.4</v:textbox>
  </v:rect>
</T>差别就v:rect于rect我想要成后面这个该怎么处理

解决方案 »

  1.   

    如果我想要,生成这样的xml标签用字符串硬拼起来快,还用XmlDocument对象来处理快
      

  2.   


    应该xmlDocument会快些。。
      

  3.   

    v 表示 namespace ,如果要 v:xxx 的效果。你应该给 XmlElement 加上 namespace 而不是硬写。
      

  4.   

    XmlElement rect = doc.CreateElement("v", "rect", "v");
    XmlElement txt = doc.CreateElement("v", "textbox", "v");
      

  5.   

    正解.
    看看CreateElement重载方法
      

  6.   

    这样不行
    生出来结果是这样<?xml version="1.0" encoding="GB2312"?>
    <T xmlns:v="urn:schemas-microsoft-com:vml">
      <v:rect style="left:175;top:175;width:175;height:175" filled="false" xmlns:v="v">
        <textbox style="color:Blue; font-size:9">28.4</textbox>
      </v:rect>
    </T>v:rect出来了,但多了xmlns:v="v"
      

  7.   

    你可以利用XmlSerializer将类进行序列化,这样可以直接生成XML文件。可以试试。与Serializable用法类似。
      

  8.   


                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
                doc.AppendChild(dec);            XmlElement t = doc.CreateElement("T");
                t.SetAttribute("xmlns:v", "urn:schemas-microsoft-com:vml");
                doc.AppendChild(t);            int gridWidth = 350;
                int halfWidth = gridWidth / 2;
                int x, y;
                x = y = halfWidth;
                int w, h;
                w = h = halfWidth;
                string attribute;
                XmlElement rect = doc.CreateElement("v", "rect", "urn:schemas-microsoft-com:vml");
                attribute = "left:" + x + ";top:" + y + ";width:" + w + ";height:" + h;
                rect.SetAttribute("style", attribute);
                rect.SetAttribute("filled", "false");
                //rect.SetAttribute("stroked", "false");
                t.AppendChild(rect);
                XmlElement txt = doc.CreateElement("v", "textbox", "urn:schemas-microsoft-com:vml");
                attribute = "color:Blue; font-size:9";
                txt.SetAttribute("style", attribute);
                txt.InnerText = "28.4";
                rect.AppendChild(txt);
                Console.Write(doc.OuterXml);
                Console.ReadKey();