【问题标题】:XElement add prefix onlyXElement 仅添加前缀
【发布时间】:2011-05-12 07:57:24
【问题描述】:

我有一个 XML 文件,例如:

<myPrefix:Catalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:sys="clr-namespace:System;assembly=mscorlib" 
 xmlns:myPrefix="clr-namespace:........">

  <myPrefix:Item Name="Item1" Mode="All" />
  <myPrefix:Item Name="Item2" Mode="Single" />

</myPrefix:Catalog>

使用 C# 我创建了一个新项目,例如:

XContainer container = XElement.Parse(xml);
XElement xmlTree = 
   new XElement("Item",
      new XAttribute("Name", item.Name),
      new XAttribute("Mode", item.Mode));

如您所见,我没有添加“myPrefix”前缀。谁能告诉我我该怎么做?我不想再次声明 xmlns。谢谢,彼得

【问题讨论】:

    标签: c# .net xml linq-to-xml


    【解决方案1】:
        XElement container = XElement.Parse(xml);
        XNamespace myPrefix = container.GetNamespaceOfPrefix("myPrefix");
    
        XElement xmlTree = new XElement(myPrefix + "Item",     
                                new XAttribute("Name", item.Name), 
                                new XAttribute("Mode", item.Mode));
    
        container.Add(xmlTree);
    

    【讨论】:

      【解决方案2】:

      编辑 1:
      如果您将命名空间属性也添加到元素,这将强制它添加前缀。但是您最终仍然会在节点中使用 xmlns 属性。 要删除它,正如 Jeff 所说,您可能需要使用 XmlWriter。

      编辑 2:
      要获得所需的 EXACT XML,您还需要创建根元素:

      编辑 3:
      好的。我找到了一种无需 XmlWriter 即可获得所需内容的方法:

      var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";
      
      XNamespace presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
      XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
      XNamespace mscorlib = "clr-namespace:System;assembly=mscorlib";
      XNamespace myPrefix = "clr-namespace:.......";
      
      XElement container = XElement.Parse(xml);
      
      var xmlTree = new XElement("Item",
                     new XAttribute("Name", "Item2"),
                     new XAttribute("Mode", "Single"));
      
      container.Add(xmlTree);
      
      foreach (var el in container.DescendantsAndSelf())
      {
        el.Name = myPrefix.GetName(el.Name.LocalName);
        var atList = el.Attributes().ToList();
        el.Attributes().Remove();
        foreach (var at in atList)
        {
          if (el.Name.LocalName == "Catalog" && at.Name.LocalName != "xmlns")
            continue;
          el.Add(new XAttribute(at.Name.LocalName, at.Value));
        }
      }
      
      container.Add(new XAttribute(XNamespace.Xmlns + "x", xaml));
      container.Add(new XAttribute(XNamespace.Xmlns + "sys", mscorlib));
      container.Add(new XAttribute(XNamespace.Xmlns + "myPrefix", myPrefix));
      

      编辑 4:
      显然有一种更简单的方法......更容易......查看其他答案。

      【讨论】:

      • 感谢您的回答,但它不起作用。结果类似于:
      • @Peter:如果元素是根,就会出现这种情况。如果它不是根,那么如果命名空间是在祖先中声明的,那么它将作为前缀。
      • @Peter:这就是 LINQ to XML 序列化程序的工作方式。我不知道是否有可能不返回并使用XmlWriter
      • @Jeff,嗯,好的,谢谢。也许有人知道一种方法,否则我会尝试 XmlWriter。
      • @Sani 非常感谢!它对我有用:) 现在我只需要删除 xmlns。
      【解决方案3】:

      您需要在命名空间中构造任何新元素。假设您知道 XML 示例中所需命名空间的前缀,然后按如下方式执行:

          var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";
      
          XElement catalog = XElement.Parse(xml);
      
          XNamespace myP = catalog.GetNamespaceOfPrefix("myPrefix");
      
          catalog.Add(new XElement(myP + "Item", new XAttribute("Name", "foo"), new XAttribute("Mode", "bar")));
      

      【讨论】:

      • 非常感谢这个不错的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      相关资源
      最近更新 更多