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我想要成后面这个该怎么处理
应该xmlDocument会快些。。
XmlElement txt = doc.CreateElement("v", "textbox", "v");
看看CreateElement重载方法
生出来结果是这样<?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"
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();