question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TimonYang-MSFT commented

C# Dataset WriteXml generate xml file but there column name getting changed

I am looking for help to understand what logic is behind column name in xml file which is generated by DataSet WriteXml() function.

Here is small program which generate xml file by DataSet WriteXml()

 List<string> prd = new List<string>();
             prd.Add("2010 FYA");
             prd.Add("2011 FYA");
             prd.Add("2012 FYA");
             prd.Add("2013 FYA");
             prd.Add("1Q 2014A");
             prd.Add("2Q 2014A");
             prd.Add("3Q 2014A");
             prd.Add("4Q 2014A");
             prd.Add("2014 FYA");
    
             DataSet dsQcViewall = new DataSet();
             DataTable dtQcViewAllSave = new DataTable();
    
             DataColumn dcSection = new DataColumn();
             dcSection.ColumnName = "Section ";
             dcSection.DataType = Type.GetType("System.String");
    
             DataColumn dcLi = new DataColumn();
             dcLi.ColumnName = "LineItem";
             dcLi.DataType = Type.GetType("System.String");
    
             DataColumn dcReviseDate = new DataColumn();
             dcReviseDate.ColumnName = "Revise Date";
             dcReviseDate.DataType = Type.GetType("System.String");
    
             dtQcViewAllSave.Columns.Add(dcSection);
             dtQcViewAllSave.Columns.Add(dcLi);
             dtQcViewAllSave.Columns.Add(dcReviseDate);
    
             for (int i = 0; i <= prd.Count-1; i++)
             {
                 DataColumn dcperiod = new DataColumn();
                 dcperiod.ColumnName = prd[i];
                 dtQcViewAllSave.Columns.Add(dcperiod);
                 dcperiod.Dispose();
                 dcperiod = null;
             }
    
             DataRow dr= dtQcViewAllSave.NewRow();
             dr["Section "] = "MS";
             dr["LineItem"] = "Morgan Stanley";
             dr["Revise Date"] = "10-09-2020";
    
             for (int i = 0; i <= prd.Count - 1; i++)
             {
                 dr[prd[i]] = 1200;
             }
             dtQcViewAllSave.Rows.Add(dr);
             dsQcViewall.Tables.Add(dtQcViewAllSave);
             dsQcViewall.WriteXml(@"d:\test.xml");


The xml file got generated and from there i got this xml code. please have a look.

 <?xml version="1.0" standalone="yes"?>
 <NewDataSet>
   <Table1>
     <Section_x0020_>MS</Section_x0020_>
     <LineItem>Morgan Stanley</LineItem>
     <Revise_x0020_Date>10-09-2020</Revise_x0020_Date>
     <_x0032_010_x0020_FYA>1200</_x0032_010_x0020_FYA>
     <_x0032_011_x0020_FYA>1200</_x0032_011_x0020_FYA>
     <_x0032_012_x0020_FYA>1200</_x0032_012_x0020_FYA>
     <_x0032_013_x0020_FYA>1200</_x0032_013_x0020_FYA>
     <_x0031_Q_x0020_2014A>1200</_x0031_Q_x0020_2014A>
     <_x0032_Q_x0020_2014A>1200</_x0032_Q_x0020_2014A>
     <_x0033_Q_x0020_2014A>1200</_x0033_Q_x0020_2014A>
     <_x0034_Q_x0020_2014A>1200</_x0034_Q_x0020_2014A>
     <_x0032_014_x0020_FYA>1200</_x0032_014_x0020_FYA>
   </Table1>
 </NewDataSet>

1) see this column Section_x0020_
actual column name was Section then why WriteXML function append x0020 after Section

2) see this one <_x0032_010_x0020_FYA>1200</_x0032_010_x0020_FYA>

i have a column 2010 FYA why WriteXml() change it to _x0032_010_x0020_FYA ?

what is logic begin after x0032 and 010 and x0020_

how do i recognize that which column is 2010 FYA ?

which logic WriteXML is using to append x0032 and 010 and x0020_ ?

please help me to understand the column naming convention. thanks




dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The XML serializer is forced to encode the column names because the XML node names are invalid. x0032_010 is 2010. x0020 is a space.

1 Vote 1 ·

How can you say x0032_010 is 2010 ? what logic is there ?

thanks

0 Votes 0 ·

@TZacks-2728
This is related to the ASCII table, x0032_ is 0x32, its decimal value is 50, which corresponds to the number 2 in the ASCII table.

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

According to the naming rules of Xml, the element name cannot start with a number, and the element name cannot contain spaces.

Consider using camel case nomenclature or adding underscores to rename your elements.

Names of Declared XML Elements and Attributes (Visual Basic)

XML Naming Rules


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.