Using the ":" symbol in XDocument to generate Excel - C #

I am new to the programming world, so this question may be silly, but I was stuck and was hoping to get some help.

I use XDocument to modify and add information to an Excel worksheet (which is an XML document) in a project to get an Excel report from Autodesk Revit. A worksheet document consists of worksheet information, including rows and cells in this 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> 

The corresponding piece of code is in the row element. The spans attribute attribute has a value of "1:11", and where my problem is. This will not allow me to enter the character ':' as an attribute value. I searched the network and found that it had something to do with the namespace declaration at this link: "Character ':', hexadecimal value 0x3A, cannot be included in the name"

However, getting this ':' character into an attribute value is required for the Excel document to work. I create a line item as follows:

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

I do not understand why this does not allow me to put ':' in the value of XAttribute, since this is just a string. Is there any way to make this work?

I tried adding the string β€œ1:11” to the XMLAttribute using XMLDocument. It really works, but I can't believe it is not possible with XDocument.

Thank you in advance

+6
source share
1 answer

You must include : in your element names. Now it is allowed. If you see someone doing this, then : it is not part of the element name, it is a separator between the namespace and the element name. So, to get the following XML file:

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

You need to write something like:

 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()) ) ) ); } } } 
0
source

All Articles