试试这个:
XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("l", "urn:LonminFRConfig");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
XmlElement config = xmlDoc.CreateElement("l:config", nsmgr.LookupNamespace("l"));
XmlAttribute schemaLocation = xmlDoc.CreateAttribute(
"xsi:schemaLocation", nsmgr.LookupNamespace("xsi"));
config.Attributes.Append(schemaLocation);
schemaLocation.Value = "urn:LonminFRConfig lonminFRConfigSchema.xsd";
xmlDoc.AppendChild(config);
xmlDoc.Save(Console.Out);
祝你好运!
LookupNamespace是相当昂贵的,当你需要创建很多特别是从头开始,你已经知道你要使用的别名和命名空间创建时的元素。 – AnthonyWJones 2009-10-14 15:00:37
@AnthonyWJones:ty为您的信息!我只是通过反射器阅读LookupNamespace实现,并且在内部使用Dictionary <>,所以这很快,但不会比const字符串快,当然=) – 2009-10-14 15:11:43
这就像一个魅力,感谢鲁本斯! – willem 2009-10-14 15:16:19