0

Suppose I have this XML file:

 <functionList>
<function name="a" id="11" >
  <InputParameters>
   <Value>
    <Range>
      <DiscreteRange>
        <DiscreteValue value="0" description="Stream 1" />
        <DiscreteValue value="1" description="Stream 2" />
      </DiscreteRange>
    </Range>
   </Value>
  </InputParameters>
  <ReturnValues>
    <Status type="BYTE"  description="0 successful, error otherwise">
      <Range>
        <SequenceRange min="0x00" max="0xFF"/>
      </Range>
    </Status>
    <Value type="UWORD32"  description="Stream select setting">
      <Range>
        <DiscreteRange>
          <DiscreteValue value="0" description="Stream 1" />
          <DiscreteValue value="1" description="Stream 2" />
        </DiscreteRange>
      </Range>
    </Value>
  </ReturnValues>
</functions>

<function name="b" id="12" >
  <InputParameters>
   <Value>
    <Range>
      <SequenceRange min="0x00" max="0xFF"/>
    </Range>
   </Value>
  </InputParameters>
  <ReturnValues>
    <Status type="BYTE"  description="0 successful, error otherwise">
      <Range>
        <SequenceRange min="0x00" max="0xFF"/>
      </Range>
    </Status>
    <Value type="UWORD32"  description="Stream select setting">
      <Range>
        <DiscreteRange>
          <DiscreteValue value="0" description="Stream 1" />
          <DiscreteValue value="1" description="Stream 2" />
        </DiscreteRange>
      </Range>
    </Value>
  </ReturnValues>
</functions>

<function name="c"  id="13"  >
  <InputParameters></InputParameters>
  <ReturnValues>
    <Status type="BYTE"  description="0 successful, error otherwise">
      <Range>
        <SequenceRange min="0x00" max="0xFF"/>
      </Range>
    </Status>
  </ReturnValues>
</function>

In general my XML Structural Look Like this:

 <functionList  >
<function  name=""  id="">
  <InputParameters>
    <!--Optional -->
    <Value type="" description="">
      <Range>

        <!--OR-->
        <DiscreteRange>
          <DiscreteValue value="" description="" />
          <DiscreteValue value="" description="" />
          <!--...-->
        </DiscreteRange>

        <!--OR-->
        <SequenceRange min="" max=""/>

        <!--OR-->
        <StringRange characters=""/>

        <!--OR-->
        <CharRange/>
      </Range>
    </Value>
    <!-- <Value type="" description="">...-->
         <!-- </Value>-->

    <!-- <Value type="" description="">...-->
    <!-- </Value>-->

    <!-- <Value type="" description="">...-->
    <!-- </Value>-->
         <!--...-->

  </InputParameters>

  <ReturnValues>
    <Status type=""  description="">
      <Range>

        <!--OR-->
        <DiscreteRange>
          <DiscreteValue value="" description="" />
          <DiscreteValue value="" description="" />
          <!--...-->
        </DiscreteRange>

        <!--OR-->
        <SequenceRange min="" max=""/>

        <!--OR-->
        <StringRange characters=""/>

        <!--OR-->
        <CharRange/>
      </Range>
    </Status >

    <!--Optional -->
    <Value type="" description="">
      <Range>

        <!--OR-->
        <DiscreteRange>
          <DiscreteValue value="" description="" />
          <DiscreteValue value="" description="" />
          <!--...-->
        </DiscreteRange>

        <!--OR-->
        <SequenceRange min="" max=""/>

        <!--OR-->
        <StringRange characters=""/>

        <!--OR-->
        <CharRange/>
      </Range>
    </Value>
    <!-- <Value type="" description="">...-->
    <!-- </Value>-->

    <!-- <Value type="" description="">...-->
    <!-- </Value>-->

    <!-- <Value type="" description="">...-->
    <!-- </Value>-->
    <!--...-->

  </ReturnValues>
</function>

<!--function...-->

As far as I understand using a built in C# deserialize does not work, because There is no a fixed structure to the file.

Does anyone have an idea how I create a hierarchy C# objects (deserialize) base on this structure?

If it's possible I'd glad to see an example, or maybe a tutorial how to build such a thing.

cheziHoyzer
  • 4,803
  • 12
  • 54
  • 81
  • 2
    Check out this question and answer about how to deserialize arbitrary xml files with data http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document/364410#364410 – Blablablaster Mar 17 '13 at 16:45
  • I've already seen it, but it's no similar cases, where there is a fixed structure, but here I have a changed structure, it is the whole problem here!! – cheziHoyzer Mar 17 '13 at 16:50
  • 1
    It appears that you *do* have a fixed structure. Many tags are simply optional. You should be able to deserialize using the linked approach provided you're comfortable having all of your "OR"s represented as optional tags. – mikey Mar 17 '13 at 17:06
  • mikey, look at the "range" tag, there are several different option inside. – cheziHoyzer Mar 17 '13 at 18:01

1 Answers1

4

Since you don't have a scheme definition file you could code it by yourself. XmlDocument works great here.

Its not a working example. But you will get the clue.

public class Document
{
    public static Document Create(Stream input)
    {
        var doc = new XmlDocument();
        doc.Load(input);
        return new Document(doc);
    }

    public class Function
    {
        public string Name { get; set; }

        public string Id { get; set; }

        public Function(XmlNode node)
        {
            Name = node.Attributes["name"].ToString();
            Id = node.Attributes["id"].ToString();
        }

        // Return values

        // Parameters
    }

    public List<Function> Functions { get; set; }

    public Document(XmlDocument doc)
    {
        Functions = new List<Function>();
        var list = doc.GetElementsByTagName("functionlist");
        XmlNode root = list[0];

        XmlNodeList children = root.ChildNodes;

        foreach (var child in children)
        {
            Functions.Add(new Function(child));
        }
    }
}

It is basically traversing XmlNode in XmlNodeLists fetching the Attributes and InnerValues of those nodes.

Martin Komischke
  • 1,440
  • 1
  • 13
  • 24
  • For the OP, you can use XDocument / XElement in this OOP example. Instead of writing a similar example with those, I'm endorsing makomweb's. If you care to see examples, click my name and then in the search box with my user id, add XElement and you'll see all the posts I've made for XML with XElement. – Chuck Savage Mar 18 '13 at 17:36