C# 未输出XmlDocument前缀

C# 未输出XmlDocument前缀,c#,xmldocument,C#,Xmldocument,我试图在一个新的XMLDocument中的几个xmlnodes上添加一个前缀(100%从头创建,不是从文件加载等) 用最简单的话来说,我有: XmlDocument doc = new XmlDocument(); XmlElement RootElement = (XmlElement)doc.AppendChild(doc.CreateElement("root")); foreach (string line in CSV) { XmlElement navPointElement

我试图在一个新的XMLDocument中的几个xmlnodes上添加一个前缀(100%从头创建,不是从文件加载等)

用最简单的话来说,我有:

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");
我确信它很简单,但我找不到任何关于它的文档。xmldocument prefix的MSDN文档只是简单地添加了前缀,与我在不需要名称空间的情况下所做的基本相同(或者至少他们在代码示例中是这样显示的)


非常感谢您的帮助:)

我遇到了类似的问题,我发现内置的.NET System.XML对象无法完成我需要的任务

我需要使用NAXML标记在POS系统中创建燃油价格变化记录一些元素需要“nax”前缀,而其他元素则不需要。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>");
System.Text.StringBuilder sb=新的StringBuilder(“\r\n”);
sb.追加(“\r\n”);
sb.追加(“\r\n”);
sb.Append(“+StoreNumber.ToString()+”\r\n”);
sb.追加(“\r\n”);
剪
某人加上(“”);

我也遇到了类似的问题,我发现内置的.NET System.XML对象无法满足我的需要

我需要使用NAXML标记在POS系统中创建燃油价格变化记录一些元素需要“nax”前缀,而其他元素则不需要。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>");
System.Text.StringBuilder sb=新的StringBuilder(“\r\n”);
sb.追加(“\r\n”);
sb.追加(“\r\n”);
sb.Append(“+StoreNumber.ToString()+”\r\n”);
sb.追加(“\r\n”);
剪
某人加上(“”);

嗯,您确实需要一个名称空间。像
这样的东西本身是无效的,因为
acp
不映射到任何名称空间,这是前缀应该做的

您需要做的是为
类型
元素调用CreateElement时要添加的元素设置名称空间

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”
属性(因为该前缀不在范围内)。将其添加到父元素中使得将其添加到子元素中变得不必要。

那么,您确实需要一个名称空间。像
这样的东西本身是无效的,因为
acp
不映射到任何名称空间,这是前缀应该做的

您需要做的是为
类型
元素调用CreateElement时要添加的元素设置名称空间

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”
属性(因为该前缀不在范围内)。将其添加到父元素中会使将其添加到子元素中变得不必要。

哦,天哪!希望这不是最终的解决办法!此时,我正在编写的应用程序只是将CSV转换为XML文档,并将其输出到windows应用程序的文本框中,然后我将其复制并粘贴到其他位置(rinky dink More?)。因此,就目前而言,我只是在记事本++中快速替换字符串以添加前缀。哦,天哪!希望这不是最终的解决办法!此时,我正在编写的应用程序只是将CSV转换为XML文档,并将其输出到windows应用程序的文本框中,然后我将其复制并粘贴到其他位置(rinky dink More?)。因此,就目前而言,我只是在notepad++中快速替换字符串以添加前缀.Works。将您的标记为答案:)谢谢您的帮助!作品将您的标记为答案:)谢谢您的帮助!