Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 35
Error in page 3 46
Data is not showing from images 15 37
Receiving a string from a WebService Push 21 36
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
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…

910 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

26 Experts available now in Live!

Get 1:1 Help Now