为什么我使用LINQ to XML获取额外的xmlns =“”?

时间:2010-05-27 15:36:48

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

我正在使用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>

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

全部谢谢。

2 个答案:

答案 0 :(得分:2)

由于缺少名称空间:

new XElement("StationInventory"... 

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

new XElement(ns + "StationInventory"...

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

答案 1 :(得分:0)

想添加到彼得·里尔沃尔德的答案中。

XML属性的XName不需要名称分隔

除了将字符串强制转换为Xname之外: 在将“ {myNamespaseName} nodeName” 强制转换为 XName 时, {myNamespaseName} 将强制转换为 XNamespase 。 >

另外,请看一下简化构造函数方法阅读的代码结构:

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"
        )
    );
}
相关问题