well format xml

Hi,

 I get an xml file, which is not well formed, it's structure is like:

<groups>
<group>
<name></name>
<desc></desc>
<items>
 <item>
   <name></name>
   <desc></desc>
 </item>
</items>
</group>
</groups>

As you can see, the problem is, that some nodes are defined in different mapping types. I can't do anything  with the xml input as I don't generate it. How can I well format it without much of work In fact I need to load it into a dataset.

My personal idea was take a regular expression and globally replace it with something <group:name> <item:name> or maybe <groups-group-name> and <groups-group-items-item-name> would be more reg exp friendly to implement. However I would be quite newbie to the reqular expressions, so I don't have any idea how to do it.

Does anybody have simple solution to read such xml into a dataset Thanks



Answer this question

well format xml

  • cjsoftuk

    Hi,

    XSLT is really worth looking into. There will be a learning curve as you also need to know a little about xpath. But if you know XSLT you can transform XML into just about anything... spreadsheet, word document, xhtml, csv, you name it.

    Here's a sample chapter from XML bible that you'll want to read.

    http://www.ibiblio.org/xml/books/bible2/chapters/ch17.html



  • dart_board

    You can load a schema definition into the data set before reading the Xml.

    DataSet xmlDataSet = new DataSet();
    xmlDataSet.ReadXmlSchema("groups.xsd");
    xmlDataSet.ReadXml("groups.xml");

    Here is a schema that matches your xml sample

    < xml version="1.0" encoding="utf-16" >
    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="
    http://www.w3.org/2001/XMLSchema">
    <xs:element name="groups">
    <xs:complexType>
    <xs:sequence>
    <xs:element maxOccurs="unbounded" name="group">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="desc" type="xs:string" />
    <xs:element name="items">
    <xs:complexType>
    <xs:sequence>
    <xs:element maxOccurs="unbounded" name="item">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="desc" type="xs:string" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>


  • Khaja Imtiyaz

    Thanks for your ideas.

    Greg4, you're right, but this was a typo mistake and have I already corrected it.

    I'm trying to use DataSet.ReadXml() but it throws me this error - the same, which throws VS when trying to view the xml in the table view. I thought the problem is, that the <name> and other tags are both in <group> and both in <item> nodes.

    Hm, XSLT would be very nice, however I've never done anything with it. Is there any tutorial

    Thanks.


  • Andr&amp;#233; Scaravelli

  • noPCtoday

    Very nice. What would you suggest where can I learn how to create and work with XSL
  • j_reins

    The only problem with your Xml is the <group> node is not closed i.e. the second <group> node should be </group>

    To create a dataset from the Xml you should use System.Xml.XmlDataDocument. There is an example here:

    http://www.ondotnet.com/pub/a/dotnet/2003/03/31/xmldatadocument.html


  • Duddy

    Hi,

    Xml that is stored in and saved from a dataset has a very flat hierarchy. Your document is well formed it's just not structured correctly. What you'll need to do with the xml in order to read it into a dataset would be to have the xml like this

    <groups>
    <group>
    <name></name>
    <desc></desc>
    <group>

    <item>
    <name></name>
    <desc></desc>
    </item>
    <item>
    <name></name>
    <desc></desc>
    </item>

    </groups>


    This would load the single group into the group table and both items into an item table. The best way to restructure your xml is through xslt rather than regular expressions. You could generate id's and link the items back to the group with xslt too.

    You could even seperate the elements using namespaces. I have no idea how this would load into a dataset though.



  • well format xml