CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Array in XML

  1. #1
    Join Date
    Jun 2005
    Posts
    66

    Array in XML

    Hi,

    I want to create a typed dataset to keep my data model in a c# application. The data shall be stored in a XML file like this:
    Code:
    <data>
      <item1 attr="1">
        <item2>1</item2>
        <item2>2</item2>
        <item2>3</item2>
        <item2>4</item2>
      </item1>
      
      <item1 attr="2">
        <item2>5</item2>
        <item2>6</item2>
        <item2>7</item2>
        <item2>8</item2>
      </item1>
    </data>
    After having created a scheme with xsd I expected to access the members of the dataset as stated below:
    Code:
      test = ds.item1[1].item2[3];
    However xsd does not build item2 as a child of item1. Instead my expectation item2 is accessible by
    Code:
      test = ds.item2[7];
    Hence, there is no information about item1 (e.g. attribute, other elements, etc.) linked to item2.

    Is there anyone who knows how to create an array member that is in scope of another array?

    Thanks in advance,
    gbr

  2. #2
    Join Date
    Apr 2006
    Posts
    17

    Re: Array in XML

    test = ds.item1[1].item2[3];

    this means u want a table item 1 with a field item 2 ? but in your xml you need more than one field item 2 , so u can not put them in the same table

    u need something like this:

    a table item1, with attr
    and a table item 2
    with a relation between them one to many

    grtz

  3. #3
    Join Date
    Jun 2005
    Posts
    66

    Re: Array in XML

    I am not sure if I understand your suggestion. Could you please provide a XML sample or a snippet of code?

    Thanks.

  4. #4
    Join Date
    Apr 2006
    Posts
    17

    Re: Array in XML

    this will not entirely solve you problem, because i don't have the time to test it out and such, i just made you a short xsd file, so you can understand what i mean:

    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema id="Dataset1" targetNamespace="http://tempuri.org/Dataset1.xsd" elementFormDefault="qualified"
    attributeFormDefault="qualified" xmlns="http://tempuri.org/Dataset1.xsd" xmlns:mstns="http://tempuri.org/Dataset1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Dataset1" msdata:IsDataSet="true">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:element name="Item1">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="id" type="xs:string" minOccurs="0" />
    </xs:sequence>
    <xs:attribute name="attr" type="xs:string" />
    </xs:complexType>
    </xs:element>
    <xs:element name="Item2">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="id" type="xs:string" minOccurs="0" />
    <xs:element name="Value" type="xs:string" minOccurs="0" />
    <xs:element name="item1Id" type="xs:string" minOccurs="0" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    <xs:key name="Dataset1Key1">
    <xs:selector xpath=".//mstns:Item1" />
    <xs:field xpath="mstns:id" />
    </xs:key>
    <xs:keyref name="Item1Item2" refer="Dataset1Key1" msdata:ConstraintOnly="true">
    <xs:selector xpath=".//mstns:Item2" />
    <xs:field xpath="mstns:item1Id" />
    </xs:keyref>
    <xs:key name="Dataset1Key2">
    <xs:selector xpath=".//mstns:Item2" />
    <xs:field xpath="mstns:id" />
    </xs:key>
    </xs:element>
    </xs:schema>



    to browse your dataset, to it like this:

    TypedDataset1 s = new TypedDataset1 ();
    foreach(DataRow row in s.Item1[0].GetChildRows(s.Relations[0]))
    {
    TypedDataset1 .Item2Row item2row = (TypedDataset1 .Item2Row)row;
    string s = item2row.Value;
    }

    i you work with my example, you will have to change your input xml,if you can't change your incoming xml file, than you will have to modify the xsd

  5. #5
    Join Date
    Oct 2005
    Location
    India
    Posts
    24

    Re: Array in XML

    Hi ,

    Try using of attributes to indicate the array index values.Like


    <root>
    <item>
    <it id="1">sample</it>
    <it id="2">sample2</it>
    </item>
    </root>

  6. #6
    Join Date
    Jun 2005
    Posts
    66

    Re: Array in XML

    Try using of attributes to indicate the array index values.Like
    That does not work either since it does not create a typed dataset I can access through
    test = ds.item1[1].item2[3];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured