2012-05-29 28 views
1

我想添加一个前缀到一个新的XMLDocument(从头开始创建的100%,而不是从文件加载等)几个xmlnodes。XmlDocument前缀不被输出

简单的说我有这样的:

XmlDocument doc = new XmlDocument(); 
XmlElement RootElement = (XmlElement)doc.AppendChild(doc.CreateElement("root")); 
foreach (string line in CSV) 
{ 
    XmlElement navPointElement = (XmlElement) RootElement.AppendChild(doc.CreateElement("navPoint")); 
    XmlElement navPointTypeElement =(XmlElement) navPointElement.AppendChild(doc.CreateElement("type")); 
    navPointTypeElement.Prefix = "acp"; 
    navPointTypeElement.InnerText = nodeCount == 0 ? "cover" : "article"; 
} 

还有更多的代码,但是这给你的我在做什么的想法。现在文档输出很好,但是它完全跳过了前缀声明。我已经阅读过关于定义命名空间的内容,但我试图通过以下方法无济于事。

XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); 
ns.AddNamespace("acp", "http://www.namespace.com"); 

我确定这很简单,但我无法完全找到任何文档。 xml文档前缀的MSDN文档只是简单地添加了前缀,与我已经完成的相同,不需要名称空间(或至少这是他们如何在代码示例中显示它)。

任何帮助是非常赞赏:)

回答

4

那么,你需要一个命名空间。像<acp:type/>之类的东西本身是无效的,因为acp不映射到任何名称空间,这是前缀应该做的。

您需要做的是在调用CreateElement的type元素时,在要添加的元素上设置名称空间。

public class StackOverflow_10807173 
{ 
    public static void Test() 
    { 
     XmlDocument doc = new XmlDocument(); 
     XmlElement RootElement = (XmlElement)doc.AppendChild(
      doc.CreateElement("root")); 
     string[] CSV = "hello world how are you".Split(' '); 
     int nodeCount = 0; 
     XmlAttribute xmlnsAttr = doc.CreateAttribute(
      "xmlns", "acp", "http://www.w3.org/2000/xmlns/"); 
     string acpNamespace = "http://www.namespace.com"; 
     xmlnsAttr.Value = acpNamespace; 
     RootElement.Attributes.Append(xmlnsAttr); 
     foreach (string line in CSV) 
     { 
      XmlElement navPointElement = (XmlElement)RootElement.AppendChild(
       doc.CreateElement("navPoint")); 
      XmlElement navPointTypeElement = (XmlElement)navPointElement.AppendChild(
       doc.CreateElement("type", acpNamespace)); // namespace here 
      navPointTypeElement.Prefix = "acp"; 
      navPointTypeElement.InnerText = nodeCount == 0 ? "cover" : "article"; 
     } 

     Console.WriteLine(doc.OuterXml); 
    } 
} 

一注意:你不需要在根元素中添加命名空间;只是如果你不这样做,那么在所有type元素中都会有xmlns:acp="yournamespace"属性(因为该前缀不在范围内)。在父元素中添加它使得不必要地将其添加到子元素中。

+0

作品。标记为答案:)感谢您的帮助! – MindingData

0

我也有类似的问题,我发现内置的.NET了System.XML对象是不能做什么,我需要。

我需要使用NAXML标记在我们的POS系统中创建燃料价格变化记录。 一些的元素需要一个“nax”前缀,而其他人没有。 System.Xml对象似乎要将其添加到全部元素或。我无法将它应用到我需要的元素上。

因为System.XML对象没有给我所需的粒度控制,所以我最终不得不使用System.Text.StringBuilder手动写出Xml。从我的应用程序

示例代码给你如何做到这一点的想法:

System.Text.StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"); 
sb.Append("<FuelPriceMaintenanceRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.POSVENDOR.com/NAXML-Extension\" xmlns:nax=\"http://www.naxml.org/POSBO/Vocabulary/2003-10-16\" xsi:schemaLocation=\"http://www.POSVENDOR.com/NAXML-Extension FuelPriceMaintenance.xsd\">\r\n"); 
sb.Append(" <nax:TransmissionHeader>\r\n"); 
sb.Append(" <nax:StoreLocationID>" + StoreNumber.ToString() + "</nax:StoreLocationID>\r\n"); 
sb.Append(" </nax:TransmissionHeader>\r\n"); 
...snip... 
sb.Append("</FuelPriceMaintenanceRequest>"); 
+0

哦,小子!希望这不是最终的解决方案!在这一点上,我正在编写的应用程序只是将CSV转换为XML文档,然后将其输出到Windows应用程序的文本框中,然后将其复制并粘贴到其他位置(rinky dink much?)。因此,我只是在记事本+ +中快速替换字符串以添加前缀。 – MindingData