【发布时间】:2012-01-09 19:31:16
【问题描述】:
以下 LINQ to XML C# 代码创建一个 XElement 和一个 XAttribute:
XElement xWorksheet =
new XElement("Worksheet", new XAttribute(XName.Get("Name", "ss"), "Sheet1"));
它生成一个内联命名空间,生成以下具有 p1 的 XML 元素:
<Worksheet p1:Name="Sheet1" xmlns:p1="ss" />
但我真正想要的是:
<Worksheet ss:Name="Sheet1" />
如何获取元素的第二种形式?
注意:如果您认识 XML 结构,我正在填写一个 Excel 电子表格,但这只是问题的轶事。
我最终在以下 Excel 文档中创建了 Worksheet 元素:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell>
<Data ss:Type="String">number </Data>
</Cell>
<Cell>
<Data ss:Type="String">name </Data>
</Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
【问题讨论】:
标签: linq-to-xml xml-namespaces