Solved

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

Posted on 2013-06-13
1
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 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