【发布时间】: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