asp.net下创建、查询、修改带名称空间的 XML 文件的例子
程序员文章站
2022-06-08 20:06:42
c#: string w3namespace = "http://www.w3.org/2000/xmlns/"; s...
c#:
string w3namespace = "http://www.w3.org/2000/xmlns/";
system.xml.xmldocument doc = new system.xml.xmldocument();
//创建根节点
system.xml.xmlnode root = doc.createnode(system.xml.xmlnodetype.element, "w", "worddocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
system.xml.xmlattribute xa;
xa = doc.createattribute("xmlns", "v", w3namespace);
xa.value = "urn:schemas-microsoft-com:vml";
root.attributes.append(xa);
//为节点添加属性
xa = doc.createattribute("xmlns", "w10", w3namespace);
xa.value = "urn:schemas-microsoft-com:office:word";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "sl", w3namespace);
xa.value = "http://schemas.microsoft.com/schemalibrary/2003/2/core";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "aml", w3namespace);
xa.value = "http://schemas.microsoft.com/aml/2001/core";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "wx", w3namespace);
xa.value = "http://schemas.microsoft.com/office/word/2003/2/auxhint";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "o", w3namespace);
xa.value = "urn:schemas-microsoft-com:office:office";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "dt", w3namespace);
xa.value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "space", w3namespace);
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("1.0", "utf-8", "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", "http://schemas.microsoft.com/office/word/2003/2/wordml");
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 = "欢迎光临【孟宪会之精彩世界】:http://dotnet.aspx.cc/";
//创建cdata节点
system.xml.xmlcdatasection xcds = doc.createcdatasection("<a href='http://dotnet.aspx.cc/'>【孟宪会之精彩世界】</a>");
node.parentnode.insertafter(xcds, node);
response.write(xcds.innertext);
doc.save(server.mappath("test.xml"));
vb.net
dim w3namespace as string = "http://www.w3.org/2000/xmlns/"
dim doc as new system.xml.xmldocument
'创建根节点
dim root as system.xml.xmlnode = doc.createnode(system.xml.xmlnodetype.element, "w", "worddocument", "http://schemas.microsoft.com/office/word/2003/2/wordml")
dim xa as system.xml.xmlattribute
xa = doc.createattribute("xmlns", "v", w3namespace)
xa.value = "urn:schemas-microsoft-com:vml"
root.attributes.append(xa)
'为节点添加属性
xa = doc.createattribute("xmlns", "w10", w3namespace)
xa.value = "urn:schemas-microsoft-com:office:word"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "sl", w3namespace)
xa.value = "http://schemas.microsoft.com/schemalibrary/2003/2/core"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "aml", w3namespace)
xa.value = "http://schemas.microsoft.com/aml/2001/core"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "wx", w3namespace)
xa.value = "http://schemas.microsoft.com/office/word/2003/2/auxhint"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "o", w3namespace)
xa.value = "urn:schemas-microsoft-com:office:office"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "dt", w3namespace)
xa.value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "space", w3namespace)
xa.value = "preserve"
root.attributes.append(xa)
'为节点增加值
dim body as system.xml.xmlnode = doc.createnode(system.xml.xmlnodetype.element, "v", "body", "urn:schemas-microsoft-com:vml")
dim childnode as system.xml.xmlnode = 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)
'添加节点声明
dim xd as system.xml.xmldeclaration = doc.createxmldeclaration("1.0", "utf-8", "yes")
doc.insertbefore(xd, doc.documentelement)
'添加处理指令
dim spi as system.xml.xmlprocessinginstruction = doc.createprocessinginstruction("mso-application", "progid=""word.document""")
doc.insertbefore(spi, doc.documentelement)
'查询节点
dim nsmanager as new system.xml.xmlnamespacemanager(doc.nametable)
nsmanager.addnamespace("w", "http://schemas.microsoft.com/office/word/2003/2/wordml")
nsmanager.addnamespace("v", "urn:schemas-microsoft-com:vml")
nsmanager.addnamespace("o", "urn:schemas-microsoft-com:office:office")
dim node as system.xml.xmlnode = doc.selectsinglenode("w:worddocument/v:body/o:t", nsmanager)
response.write(node.innertext)
node.innertext = "欢迎光临【孟宪会之精彩世界】:http://dotnet.aspx.cc/"
'创建cdata节点
dim xcds as system.xml.xmlcdatasection = doc.createcdatasection("<a href='http://dotnet.aspx.cc/'>【孟宪会之精彩世界】</a>")
node.parentnode.insertafter(xcds, node)
response.write(xcds.innertext)
doc.save(server.mappath("test.xml"))
string w3namespace = "http://www.w3.org/2000/xmlns/";
system.xml.xmldocument doc = new system.xml.xmldocument();
//创建根节点
system.xml.xmlnode root = doc.createnode(system.xml.xmlnodetype.element, "w", "worddocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
system.xml.xmlattribute xa;
xa = doc.createattribute("xmlns", "v", w3namespace);
xa.value = "urn:schemas-microsoft-com:vml";
root.attributes.append(xa);
//为节点添加属性
xa = doc.createattribute("xmlns", "w10", w3namespace);
xa.value = "urn:schemas-microsoft-com:office:word";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "sl", w3namespace);
xa.value = "http://schemas.microsoft.com/schemalibrary/2003/2/core";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "aml", w3namespace);
xa.value = "http://schemas.microsoft.com/aml/2001/core";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "wx", w3namespace);
xa.value = "http://schemas.microsoft.com/office/word/2003/2/auxhint";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "o", w3namespace);
xa.value = "urn:schemas-microsoft-com:office:office";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "dt", w3namespace);
xa.value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882";
root.attributes.append(xa);
xa = doc.createattribute("xmlns", "space", w3namespace);
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("1.0", "utf-8", "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", "http://schemas.microsoft.com/office/word/2003/2/wordml");
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 = "欢迎光临【孟宪会之精彩世界】:http://dotnet.aspx.cc/";
//创建cdata节点
system.xml.xmlcdatasection xcds = doc.createcdatasection("<a href='http://dotnet.aspx.cc/'>【孟宪会之精彩世界】</a>");
node.parentnode.insertafter(xcds, node);
response.write(xcds.innertext);
doc.save(server.mappath("test.xml"));
vb.net
dim w3namespace as string = "http://www.w3.org/2000/xmlns/"
dim doc as new system.xml.xmldocument
'创建根节点
dim root as system.xml.xmlnode = doc.createnode(system.xml.xmlnodetype.element, "w", "worddocument", "http://schemas.microsoft.com/office/word/2003/2/wordml")
dim xa as system.xml.xmlattribute
xa = doc.createattribute("xmlns", "v", w3namespace)
xa.value = "urn:schemas-microsoft-com:vml"
root.attributes.append(xa)
'为节点添加属性
xa = doc.createattribute("xmlns", "w10", w3namespace)
xa.value = "urn:schemas-microsoft-com:office:word"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "sl", w3namespace)
xa.value = "http://schemas.microsoft.com/schemalibrary/2003/2/core"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "aml", w3namespace)
xa.value = "http://schemas.microsoft.com/aml/2001/core"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "wx", w3namespace)
xa.value = "http://schemas.microsoft.com/office/word/2003/2/auxhint"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "o", w3namespace)
xa.value = "urn:schemas-microsoft-com:office:office"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "dt", w3namespace)
xa.value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882"
root.attributes.append(xa)
xa = doc.createattribute("xmlns", "space", w3namespace)
xa.value = "preserve"
root.attributes.append(xa)
'为节点增加值
dim body as system.xml.xmlnode = doc.createnode(system.xml.xmlnodetype.element, "v", "body", "urn:schemas-microsoft-com:vml")
dim childnode as system.xml.xmlnode = 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)
'添加节点声明
dim xd as system.xml.xmldeclaration = doc.createxmldeclaration("1.0", "utf-8", "yes")
doc.insertbefore(xd, doc.documentelement)
'添加处理指令
dim spi as system.xml.xmlprocessinginstruction = doc.createprocessinginstruction("mso-application", "progid=""word.document""")
doc.insertbefore(spi, doc.documentelement)
'查询节点
dim nsmanager as new system.xml.xmlnamespacemanager(doc.nametable)
nsmanager.addnamespace("w", "http://schemas.microsoft.com/office/word/2003/2/wordml")
nsmanager.addnamespace("v", "urn:schemas-microsoft-com:vml")
nsmanager.addnamespace("o", "urn:schemas-microsoft-com:office:office")
dim node as system.xml.xmlnode = doc.selectsinglenode("w:worddocument/v:body/o:t", nsmanager)
response.write(node.innertext)
node.innertext = "欢迎光临【孟宪会之精彩世界】:http://dotnet.aspx.cc/"
'创建cdata节点
dim xcds as system.xml.xmlcdatasection = doc.createcdatasection("<a href='http://dotnet.aspx.cc/'>【孟宪会之精彩世界】</a>")
node.parentnode.insertafter(xcds, node)
response.write(xcds.innertext)
doc.save(server.mappath("test.xml"))