Solved

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

Posted on 2013-06-13
1
770 Views
Last Modified: 2013-06-16
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
Comment
Question by:countrymeister
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39247622
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Groupbox Control ? 2 27
XSL FO - remove empty space and automatically size columns 6 17
xml with php question 5 29
Build a string of emails from a gridview 2 13
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question