为什么我会收到额外的 xmlns=""使用 LINQ to XML?

发布于 2024-09-03 21:50:55 字数 2020 浏览 4 评论 0 原文

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

请注意第一个子元素中的空命名空间声明。有什么想法可以解决这个问题吗?任何提示当然不胜感激。

谢谢大家。

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", plaza.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必了 2024-09-10 21:50:55

由于以下位置缺少命名空间:

new XElement("StationInventory"... 

这隐式指示 StationInvetory 元素的空命名空间“”。您应该这样做:

new XElement(ns + "StationInventory"...

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

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.

注定孤独终老 2024-09-10 21:50:55

想补充彼得·利勒沃德的答案。

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

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"
        )
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文