在XDocument中使用':'字符生成Excel – C#

我对编程世界相当陌生,所以这个问题可能是愚蠢的,但我卡住了,希望得到一些帮助。

我正在使用XDocument来修改信息并将其添加到Excel工作表(这是一个XML文档),以便从Autodesk Revit获取Excel报表。 工作表文档由工作表信息组成,包括此configuration中的行和单元格:

<row r="11" spans="1:11" x14ac:dyDescent="0.2"> <cr="A11" s="198" t="inlineStr"> <is> <t>example</t> </is> </c> <cr="B11" s="199" t="inlineStr"> <is> <t>string</t> </is> </c> <cr="C11" s="200"/> <cr="D11" s="201"/> <cr="E11" s="201"/> <cr="F11" s="202"/> <cr="G11" s="203"/> <cr="H11" s="204"/> <cr="I11" s="205"/> <cr="J11" s="205"/> <cr="K11" s="206"/> </row> 

代码的相关部分在行元素中。 跨度属性的值为“1:11”,这就是我的问题所在。 它不会让我input一个“:”字符作为属性的值。 我search了networking,发现它与这个链接中的命名空间声明有关: “':'字符,hex值0x3A,不能包含在名称中”

但是,将这个“:”字符放入属性值对于Excel文档的工作是必不可less的。 我创build行元素如下:

 XElement row = new XElement("row", new XAttribute("r", i.ToString()), new XAttribute("spans", "1:" + collumnCount.ToString()), new XAttribute("x14ac:dyDescent", "0.2")); 

我不明白为什么它不让我把':'放到XAttribute的值中,因为这只是一个string。 有什么办法可以做这个工作吗?

我已经尝试使用XMLDocument将string“1:11”添加到XMLAttribute中。 这确实奏效,但我不能相信这是不可能的XDocument。

提前致谢

你应该在你的元素名称中包含 现在允许。 如果你看到有人这样做,那不是元素名称的一部分,它是名称空间和元素名称之间的分隔符。 所以为了得到下面的XML文件:

 <rss xmlns:media="http://m.xyz.com/rss/" version="2.0"> <channel vendor="ABC" lastpublishdate="05/12/2013 01:02"/> <title>Videos</title> <media:thumbnail width="180" height="75" /> </rss> 

你必须写下如下的东西:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string x = "55"; XNamespace aw = "http://m.xyz.com/rss/"; XDocument document = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement( "rss", new XAttribute(XNamespace.Xmlns + "media", "http://m.xyz.com/rss/"), new XAttribute("version", "2.0"), new XElement("channel", new XAttribute("vendor", "ABC"), new XAttribute("lastpublishdate", DateTime.Now.ToString("dd/MM/yyyy hh:mm"))), new XElement("title", "Videos"), new XElement( aw + "thumbnail", new XAttribute("width", x.Trim()), new XAttribute("height", x.Trim()) ) ) ); } } }