Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

Remove xml tag names from xmldocument and add header elements .NET

I have a dataset that I convert to an xml document, I want to add header elements that relate to the columns of the daataset.

And then just leave the content from the original document

Original document
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <Code>ABCD</Code>
    <BusDate>2013-03-27T00:00:00-04:00</BusDate>
    <ID>16172</ID>
  </Table>
  <Table>
    <Code>EFGH</Code>
    <BusDate>2013-03-27T00:00:00-04:00</BusDate>
    <ID>16259</ID>  
  </Table>
  <Table>
    <Code>HIJK</Code>
    <BusDate>2013-03-28T00:00:00-04:00</BusDate>
    <ID>6953</ID>
  </Table>
  <Table>
    <Code>LMNO</Code>
    <BusDate>2013-03-28T00:00:00-04:00</BusDate>
    <ID>6958</ID>  
  </Table>
  </NewDataSet>

Final Document ater conversion and adding header node
  <NewDataSet>
  <Header>
    <Code/>
      <BusDate/>
      <ID/>
  </Header>
  <Row>
    <ABCD/>
    <2013-03-27T00:00:00-04:00/>
    <16172/>
  </Row>
  <Row>
    <EFGH/>
    <2013-03-27T00:00:00-04:00/>
    <16259/>  
  </Row>
  </NewDataSet>
0
countrymeister
Asked:
countrymeister
1 Solution
 
Fernando SotoCommented:
Hi countrymeister;

The rules for XML element names must not be violated to remain a well formed XML document. The rules are as follows.

XML elements must follow these naming rules:
1.  Names can contain letters, numbers, and other characters
2.  Names cannot start with a number or punctuation character
3.  Names cannot start with the letters xml (or XML, or Xml, etc)
4.  Names cannot contain spaces

Looking at one Row of the document:

  <Row>
    <ABCD/>
    <2013-03-27T00:00:00-04:00/>
    <16172/> 
  </Row>

Open in new window


From the above sample the second and third child node of Row violate the above rules. Also you should not remove the node names from the child elements of Row. The following Linq to XML code does most of what you want.

// Load the XML document from the file system
XDocument doc = XDocument.Load("C:/Working Directory/Dataset.xml");
// Create the header elements
XElement header = new XElement("Header", 
                      new XElement("Code"),
                      new XElement("BusDate"),
                      new XElement("ID"));

// Insert the herder elements just after the Root node                      
doc.Root.AddFirst(header);

// Get a reference to all the Table nodes so we can change there names
var rows = from t in doc.Descendants("Table")
           select t;

// Change the node names of <Table> to <Row>
foreach (var r in rows)
{
    r.Name = "Row";
}

Open in new window


After running the above code snippet the doc variable now looks like the following.

<NewDataSet>
  <Header>
    <Code />
    <BusDate />
    <ID />
  </Header>
  <Row>
    <Code>ABCD</Code>
    <BusDate>2013-03-27T00:00:00-04:00</BusDate>
    <ID>16172</ID>
  </Row>
  <Row>
    <Code>EFGH</Code>
    <BusDate>2013-03-27T00:00:00-04:00</BusDate>
    <ID>16259</ID>
  </Row>
  <Row>
    <Code>HIJK</Code>
    <BusDate>2013-03-28T00:00:00-04:00</BusDate>
    <ID>6953</ID>
  </Row>
  <Row>
    <Code>LMNO</Code>
    <BusDate>2013-03-28T00:00:00-04:00</BusDate>
    <ID>6958</ID>
  </Row>
</NewDataSet>

Open in new window


How do you want to handle the above concerns .
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now