【问题标题】:xmldocument add namespace prefix in same nodexmldocument 在同一节点中添加命名空间前缀
【发布时间】:2015-03-25 09:10:38
【问题描述】:

我希望输出是这样的:

<Order xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:gml="example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd"
xsi:noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">

我用来得到这个的代码是:

xmlDocument.DocumentElement.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
xmlDocument.DocumentElement.SetAttribute("xmlns:gml", "example.com");
xmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlDocument.DocumentElement.SetAttribute("xsi:schemaLocation", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");     
xmlDocument.DocumentElement.SetAttribute("xsi:noNamespaceSchemaLocation","http://www. xyz.co.uk/xmlorders3/lig_xml_orders.xsd");

但我得到这样的输出:

<Order xmlns:gml="example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       schemaLocation="example.com http://schemas.xyz.net/gml/2.1.2/feature.xsd" 
       noNamespaceSchemaLocation="http://www.xyz.co.uk/xmlorders3/lig_xml_orders.xsd">

请给我建议任何其他方式来获得所需的输出。

【问题讨论】:

标签: c# namespaces xmldocument


【解决方案1】:

您应该使用另一个版本的SetAttribute 方法:

xmlDocument.DocumentElement
    .SetAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "example.com http://schemas.opengis.com/gml/2.1.2/feature.xsd");

【讨论】:

    【解决方案2】:

    我找到了出路。这可能不是最好的方法。解决办法是:

    字符串 xmlString = xmlDocument.InnerXml.ToString(); xmlString= xmlString.Replace("schemaLocation", "xsi:schemaLocation"); xmlString= xmlString.Replace("noNamespaceSchemaLocation", "xsi:noNamespaceSchemaLocation"); xmlDocument.LoadXml(xmlString);

    // 现在我有了所需的 xmlDocument。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2011-04-28
      • 2014-08-17
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多