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

T.Zacks 3,986 Reputation points
2021-07-08T15:54:15.167+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-07-09T05:48:02.527+00:00

    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.

    0 comments No comments