XML的應用越來越廣泛了
如Vista
Flex編程都將使用 XML
正確掌握XML的各種操作
對提高編程效率至關重要
下面就是一個綜合處理帶名稱空間的XML的例子
C#
string w
NameSpace =
;
System
Xml
XmlDocument doc = new System
Xml
XmlDocument();
//創建根節點
System
Xml
XmlNode root = doc
CreateNode(System
Xml
XmlNodeType
Element
w
wordDocument
);
System
Xml
XmlAttribute xa;
xa = doc
CreateAttribute(
xmlns
v
w
NameSpace);
xa
Value =
urn:schemas
microsoft
com:vml
;
root
Attributes
Append(xa);
//為節點添加屬性
xa = doc
CreateAttribute(
xmlns
w
w
NameSpace);
xa
Value =
urn:schemas
microsoft
com:office:word
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
SL
w
NameSpace);
xa
Value =
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
aml
w
NameSpace);
xa
Value =
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
wx
w
NameSpace);
xa
Value =
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
o
w
NameSpace);
xa
Value =
urn:schemas
microsoft
com:office:office
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
dt
w
NameSpace);
xa
Value =
uuid:C
F
B
d
A
F
AA
C
;
root
Attributes
Append(xa);
xa = doc
CreateAttribute(
xmlns
space
w
NameSpace);
xa
Value =
preserve
;
root
Attributes
Append(xa);
//為節點增加值
System
Xml
XmlNode body = doc
CreateNode(System
Xml
XmlNodeType
Element
v
body
urn:schemas
microsoft
com:vml
);
System
Xml
XmlNode childNode = doc
CreateNode(System
Xml
XmlNodeType
Element
o
t
urn:schemas
microsoft
com:office:office
);
childNode
InnerText =
歡迎光臨【孟憲會之精彩世界】
;
//添加到內存樹中
body
AppendChild(childNode);
root
AppendChild(body);
doc
AppendChild(root);
//添加節點聲明
System
Xml
XmlDeclaration xd = doc
CreateXmlDeclaration(
UTF
yes
);
doc
InsertBefore(xd
doc
DocumentElement);
//添加處理指令
System
Xml
XmlProcessingInstruction spi = doc
CreateProcessingInstruction(
mso
application
progid=\
Word
Document\
);
doc
InsertBefore(spi
doc
DocumentElement);
//查詢節點
System
Xml
XmlNamespaceManager nsmanager = new System
Xml
XmlNamespaceManager(doc
NameTable);
nsmanager
AddNamespace(
w
);
nsmanager
AddNamespace(
v
urn:schemas
microsoft
com:vml
);
nsmanager
AddNamespace(
o
urn:schemas
microsoft
com:office:office
);
System
Xml
XmlNode node = doc
SelectSingleNode(
w:wordDocument/v:body/o:t
nsmanager);
Response
Write(node
InnerText);
node
InnerText =
歡迎光臨【孟憲會之精彩世界】:/
;
//創建CDATA節點
System
Xml
XmlCDataSection xcds = doc
CreateCDataSection(
<a >【孟憲會之精彩世界】</a>
);
node
ParentNode
InsertAfter(xcds
node);
Response
Write(xcds
InnerText);
doc
Save(Server
MapPath(
test
xml
));
From:http://tw.wingwit.com/Article/program/net/201311/13631.html