Solved

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

Posted on 2013-06-13
1
736 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 62

Accepted Solution

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now