简体   繁体   English

向xml节点添加前缀

[英]Adding a prefix to an xml node

Current File Format 当前文件格式

<Folio>
<Node1>Value1</Node1>
<Node2>Value2</Node2>
<Node3>Value3</Node3>
</Folio>

Desired Output 期望的输出

<vs:Folio>
<vs:Node1>Value1</vs:Node1>
<vs:Node2>Value2</vs:Node2>
<vs:Node3>Value3</vs:Node3>
</vs:Folio>

I am using XmlElement and XmlDocument to add the prefix to the child Node element and I'm unable to accomplish it. 我使用XmlElement和XmlDocument将前缀添加到子Node元素,我无法完成它。 I would be really grateful if someone could give me the right push in the right direction. 如果有人能够在正确的方向上给我正确的推动,我将非常感激。

If you are trying to add namespace to the elements after loading the xml document then it is not possible. 如果您在加载xml文档后尝试将命名空间添加到元素,则无法进行。

From MSDN: 来自MSDN:

You cannot add, modify, or delete an XML namespace definition in an instance of an XML document after the document has been loaded into the XML Document Object Model (XMLDOM) parser. 在将文档加载到XML文档对象模型(XMLDOM)解析器后,您无法在XML文档的实例中添加,修改或删除XML命名空间定义。 The XML nodes that are used to represent data in the XML document are created when the document is loaded into the XMLDOM parser. 当文档加载到XMLDOM解析器中时,将创建用于表示XML文档中的数据的XML节点。 These nodes are permanently bound to their XML namespace attributes when they are created. 这些节点在创建时永久绑定到其XML命名空间属性。 Therefore, the empty XML namespace declaration (xmlns = "") is appended to the child nodes of these nodes to preserve the default XML namespace attribute of these nodes. 因此,空XML名称空间声明(xmlns =“”)将附加到这些节点的子节点,以保留这些节点的默认XML名称空间属性。

However you can load the input, read each element and write it to another document (or in-memory) which has the namespace set. 但是,您可以加载输入,读取每个元素并将其写入具有命名空间集的另一个文档(或内存中)。 Below is the code that parses the string xml, creates a new xml element along with namespace prefix and namespace. 下面是解析字符串xml的代码,创建一个新的xml元素以及名称空间前缀和名称空间。

            String xmlWithoutNamespace =
                @"<Folio><Node1>Value1</Node1><Node2>Value2</Node2><Node3>Value3</Node3></Folio>";
            String prefix ="vs";
            String testNamespace = "http://www.testnamespace/vs/";
            XmlDocument xmlDocument = new XmlDocument();

            XElement folio = XElement.Parse(xmlWithoutNamespace);
            XmlElement folioNode = xmlDocument.CreateElement(prefix, folio.Name.LocalName, testNamespace);

            var nodes = from node in folio.Elements()
                        select node;

            foreach (XElement item in nodes)
            {
                var node = xmlDocument.CreateElement(prefix, item.Name.ToString(), testNamespace);
                node.InnerText = item.Value;
                folioNode.AppendChild(node);
            }

            xmlDocument.AppendChild(folioNode);

xmlDocument now contains the xml with each node prefixed with vs. xmlDocument现在包含xml,每个节点都以前缀为对象

 private static void SetPrefix(string prefix, XmlNode node)
    {
        node.Prefix = prefix;
        foreach (XmlNode n in node.ChildNodes)
        {
            //if (node.ParentNode != null)
            //{
            if (n.Name.Contains("QualifyingProperties"))
            {
                break;
            }
            //}
            SetPrefix(prefix, n);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM