【问题标题】:How to modify DocumentElement.Prefix in XmlDocument如何在 XmlDocument 中修改 DocumentElement.Prefix
【发布时间】:2012-06-20 14:03:32
【问题描述】:

我有一些 xml 集,我必须在没有任何前缀的节点上添加命名空间前缀。

我编写了一个代码,它适用于根元素以外的所有节点。

请指出我也可以更改根元素前缀。

 private void ReplaceFile(string xmlfile)
 {
     XmlDocument doc = new XmlDocument();
     doc.Load(xmlfile);
     var a = doc.CreateAttribute("xmlns:mailxml12tm");
     a.Value = "http://idealliance.org/Specs/mailxml12.0a/mailxml_tm";
     doc.DocumentElement.Attributes.Append(a);
     doc.DocumentElement.Prefix = "mailxml12tm";
     //foreach (XmlNode item in doc.SelectNodes("//*").Cast<XmlNode>().Where(item => item.Prefix.Length == 0))
     //{
     //    item.Prefix = "mailxml12tm";
     //}
     doc.Save(xmlfile);
}

xml文件:

<DeliveryApptCreateRequest 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    p3:ApptType="Pallet" p3:PickupOrDelivery="Delivery" 
    p3:ShipperApptRequestID="4490B0C07355" p3:SchedulerCRID="6498874" 
    xmlns:p3="http://idealliance.org/Specs/mailxml12.0a/mailxml_defs">
    <SubmitterTrackingID xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">2CAD3FBC71B1E1517021</SubmitterTrackingID>
    <DestinationEntry xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">No</DestinationEntry>
    <OneTimeAppt xmlns="http://idealliance.org/Specs/mailxml12.0a/mailxml_tm">
        <PreferredAppt>2012-07-01T09:00:00Z</PreferredAppt>
    </OneTimeAppt> 
</DeliveryApptCreateRequest>

【问题讨论】:

    标签: c# .net xml xml-parsing xmldocument


    【解决方案1】:

    你看到这个答案了吗:https://stackoverflow.com/a/2255337/219344 Jeff Sternal?

    如果您已经在根节点中声明了命名空间,那么您只需 需要更改 SetAttribute 调用以使用无前缀属性 姓名。因此,如果您的根节点定义了这样的命名空间:

    &lt;People xmlns:s='http://niem.gov/niem/structures/2.0'&gt;

    您可以执行此操作,该属性将获取您设置的前缀 已经建立:

    // no prefix on the first argument - it will be rendered as // s:id='ID_Person_01' TempElement.SetAttribute("id", "http://niem.gov/niem/structures/2.0", "ID_Person_01");

    如果您尚未声明命名空间(及其前缀),则 三字符串 XmlDocument.CreateAttribute 重载将为您完成:

    // Adds the declaration to your root node var attribute = xmlDocToRef.CreateAttribute("s", "id", "http://niem.gov/niem/structures/2.0"); attribute.InnerText = "ID_Person_01" TempElement.SetAttributeNode(attribute);

    【讨论】:

      【解决方案2】:

      您可以使用以下内容:

      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<test xmlns='123'/>");
      XmlElement e = doc.DocumentElement;       
      e.Prefix = "a";
      Console.WriteLine(doc.InnerXml);
      

      输出:

      <a:test xmlns="123" xmlns:a="123" />
      

      创立于msdn

      编辑:

      由于它不适用于您的主元素,您可以在根元素上使用.Replace("&lt;", "&lt;Prefixe:")); 作为String。它不漂亮,但它可以完成工作。

      【讨论】:

      • 但是当您保存文档时,它将保持与 test 相同,而不是 a:test
      • 请检查我的 xml 文件。我认为由于一些 namespaceuri 问题或其他原因,它无法使用它
      猜你喜欢
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2023-03-24
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多