开发者

Why am I getting the extra xmlns="" using LINQ to XML?

I'm using LINQ to XML to generate a piece of XML. Everything works great except I'm throwing in some empty namespace declarations somehow. Does anyone out there know what I'm doing incorrectly? Here is my code

    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", p开发者_Python百科laza.PlazaId)))))));

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

And here is the resulting xml. I hope it renders correctly? The browser may hide the tags.

<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>

Notice the empty namespace declaration in the first child element. Any ideas how I can remedy this? Any tips are of course appreciated.

Thanks All.


Because of the missing namespace in:

new XElement("StationInventory"... 

This implicitly indicates the empty namespace "" for the StationInvetory element. You should do:

new XElement(ns + "StationInventory"...

Note that you must do this for any element you create that lives in the ns namespace. The XML serializer will make sure to qualify elements with the correct namespace prefix according to scope.


Want to add to Peter Lillevold's answer.

XML Attributes don't require namespase in their XName

In addition to casting string to Xname: The {myNamespaseName} will be casted to XNamespase on casting of "{myNamespaseName}nodeName" to XName

Also, look to the code structure that simplifies reading of the constructor method:

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"
        )
    );
}

or

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜