【问题标题】:Why am I getting the extra xmlns="" using LINQ to XML?为什么我会使用 LINQ to XML 获得额外的 xmlns=""?
【发布时间】:2011-02-24 17:41:56
【问题描述】:

我正在使用 LINQ to XML 生成一段 XML。一切都很好,除了我以某种方式抛出一些空的命名空间声明。有谁知道我做错了什么?这是我的代码

    private string SerializeInventory(IEnumerable<InventoryInformation> inventory)
    {
        var zones = inventory.Select(c => new {
            c.ZoneId
            , c.ZoneName
            , c.Direction
        }).Distinct();

        XNamespace ns = "http://www.dummy-tmdd-address";
        XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

        var xml = new XElement(ns + "InventoryList"
                               , new XAttribute(XNamespace.Xmlns + "xsi", xsi)
                               , zones.Select(station => new XElement("StationInventory"
                               , new XElement("station-id", station.ZoneId)
                               , new XElement("station-name", station.ZoneName)
                               , new XElement("station-travel-direction", station.Direction)
                               , new XElement("detector-list"
                               , inventory.Where(p => p.ZoneId == station.ZoneId).Select(plaza =>
                               new XElement("detector", new XElement("detector-id", plaza.PlazaId)))))));

        xml.Save(@"c:\tmpXml\myXmlDoc.xml");
        return xml.ToString();
    }

这是生成的 xml。我希望它正确渲染?浏览器可能会隐藏标签。

<InventoryList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dummy-tmdd-address">
<StationInventory xmlns="">
  <station-id>999</station-id> 
  <station-name>Zone 999-SEB</station-name> 
  <station-travel-direction>SEB</station-travel-direction> 
 <detector-list>
<detector>
  <detector-id>7503</detector-id> 
 </detector>
<detector>
  <detector-id>2705</detector-id> 
 </detector>
</detector-list>
</StationInventory>
</InventoryList>

注意第一个子元素中的空命名空间声明。有什么想法可以解决这个问题吗?任何提示当然都会受到赞赏。

谢谢大家。

【问题讨论】:

    标签: c# xml namespaces linq-to-xml


    【解决方案1】:

    因为缺少命名空间:

    new XElement("StationInventory"... 
    

    这隐含地表示 StationInvetory 元素的空命名空间“”。你应该这样做:

    new XElement(ns + "StationInventory"...
    

    请注意,您必须对您创建的任何位于 ns 命名空间中的元素执行此操作。 XML 序列化程序将确保根据范围使用正确的命名空间前缀来限定元素。

    【讨论】:

      【解决方案2】:

      想补充一下 Peter Lillevold 的答案。

      XML 属性的 XName 中不需要命名空间

      除了将字符串转换为 Xname 之外: {myNamespaseName} 将在 "{myNamespaseName}nodeName" 转换为 XName 时转换为 XNamespase p>

      另外,看看简化构造方法读取的代码结构:

      private readonly XNamespace _defaultNamespace = "yourNamespace";
      
      public XElement GetXmlNode()
      {
          return
          new XElement(_defaultNamespace + "nodeName", 
              new XElement(_defaultNamespace + "nodeWithAttributes", 
                  new XAttribute("attribute1Name", "valueOfAttribute1"),
                  new XAttribute("attribute2Name", "valueOfAttribute2"),
                  "valueOfnodeWithAttributes"
              )
          );
      }
      

      public XElement GetXmlNode()
      {
          return
          new XElement("{myNamespaseName}nodeName", 
              new XElement("{myNamespaseName}nodeWithAttributes", 
                  new XAttribute("attribute1Name", "valueOfAttribute1"),
                  new XAttribute("attribute2Name", "valueOfAttribute2"),
                  "valueOfnodeWithAttributes"
              )
          );
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-20
        • 1970-01-01
        • 2015-06-20
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 2021-07-27
        • 1970-01-01
        • 2011-03-19
        相关资源
        最近更新 更多