111,125
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", ""));
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
//manager.AddNamespace("haha", "http://schemas.microsoft.com/BizTalk/2003");
XmlElement root = document.CreateElement("my:root", "haha:http://schemas.microsoft.com/BizTalk/2003");
document.AppendChild(root);
XmlElement newNode = document.CreateElement("abc","my");
newNode.InnerText = "hello";
root.AppendChild(newNode);
XmlElement newNode1 = document.CreateElement("xyz","my");
newNode1.InnerText = "world";
root.AppendChild(newNode1);
document.Save(@"d:\test.xml");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<my:root xmlns:my="haha:http://schemas.microsoft.com/BizTalk/2003">
<abc xmlns="my">hello</abc>
<xyz xmlns="my">world</xyz>
</my:root>
<my:abc>hello</my:abc>
<my:xyz>world</my:xyz>
XmlDocument document = new XmlDocument();
document.AppendChild(document.CreateXmlDeclaration("1.0", "UTF-8", ""));
string namespaceUrl = "haha:http://schemas.microsoft.com/BizTalk/2003";
XmlElement root = document.CreateElement("my", "root", namespaceUrl);
document.AppendChild(root);
XmlElement newNode = document.CreateElement("my", "abc", namespaceUrl);
newNode.InnerText = "hello";
root.AppendChild(newNode);
XmlElement newNode1 = document.CreateElement("my", "xyz", namespaceUrl);
newNode1.InnerText = "world";
root.AppendChild(newNode1);
document.Save(@"d:\test.xml");
最后得到:
<?xml version="1.0" encoding="UTF-8" ?>
<my:root xmlns:my="haha:http://schemas.microsoft.com/BizTalk/2003">
<my:abc>hello</my:abc>
<my:xyz>world</my:xyz>
</my:root>