Isael Macias Isael Macias - 3 months ago 12
C# Question

How to catch/send XML doc with various sub arrays?

I am working with some web services and I ran into some puzzle that I cannot resolve. I am working with arrays of objects within an array of another object within an array of a main object.

I am using various objects like this:


public class OBJECT1

{

public string item1;

public string item2;

public string item3;

}

public class OBJECT2

{

public string item1;

public string item2;

public OBJECT1[] item3;

}

public class OBJECT3

{

public string item1;

public string item2;

public OBJECT2[] item3;

}


So my puzzle comes when I consult a database based on one element within one of the 3 objects.

Let me explain:

When I search one element from OBJECT1, I can get 1 or more items from the other objects. The question is: how can I put all the information within a table or an XML doc or a dataset? I really do not have any clue to where to start, since I want to make a structre like this:


Object3[N]

item1

item2

Object2[1]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


Object2[2]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


Object2[3]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


.
.
.
Object2[N]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


.

.

.

Object3[N]

item1

item2

Object2[1]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


Object2[2]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


Object2[3]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]


.
.
.
Object2[N]
ítem1
item2

Object1[1]
item1
item2
item3
Object1[2]
item1
item2
item3
Object1[3]
.
.
.
Object1[N]



I really do not know where to start catching this. I already got the structure, but I need a hint as to where to start. I hope you guys can help me.

I am working with Visual Studio 2008 C# and SQL Server and web services.

I hope anyone can help me with this, thanks anyway.

Greetings.

Answer

You can use the built in DataContract functionality to turn your object graph into an XML document with the nesting that you mentioned.

Two parts:

  1. Annotate your classes with the appropriate attributes.

    [DataContract(Namespace = "DataContracts")]
    public class Foo
    {
        [DataMember]
        public string First;
    
        [DataMember]
        public string Second;
    
        [DataMember]
        public string Third;
    
    }
    
    [DataContract(Namespace = "DataContracts")]
    public class Bar
    {
        [DataMember]
        public string First;
    
        [DataMember]
        public string Second;
    
        [DataMember]
        public Foo[] ManyFoos;
    }
    
    [DataContract(Namespace = "DataContracts")]
    public class Root
    {
        [DataMember]
        public string First;
    
        [DataMember]
        public string Second;
    
        [DataMember]
        public Bar[] ManyBars;
    }
    
  2. Use the DataContractSerializer to transform your object graph into an XML document.

    var root = new Root()
    {
        ... snip, object initialization ...
    };
    
    var serializer = new DataContractSerializer(typeof(Root));
    var xmlStringBuilder = new StringBuilder();
    using (var xmlWriter = XmlWriter.Create(xmlStringBuilder, new XmlWriterSettings() { Indent = true }))
    {
        serializer.WriteObject(xmlWriter, root);
    }
    
    var xmlString = xmlStringBuilder.ToString();
    

This produces the following XML (for my example anyway).

<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="DataContracts">
  <First>Root01_First</First>
  <ManyBars>
    <Bar>
      <First>Bar01_First</First>
      <ManyFoos>
        <Foo>
          <First>Foo01_First</First>
          <Second>Foo01_Second</Second>
          <Third>Foo01_Third</Third>
        </Foo>
        <Foo>
          <First>Foo02_First</First>
          <Second>Foo02_Second</Second>
          <Third>Foo02_Third</Third>
        </Foo>
      </ManyFoos>
      <Second>Bar01_Second</Second>
    </Bar>
    <Bar>
      <First>Bar02_First</First>
      <ManyFoos>
        <Foo>
          <First>Foo03_First</First>
          <Second>Foo03_Second</Second>
          <Third>Foo03_Third</Third>
        </Foo>
      </ManyFoos>
      <Second>Bar02_Second</Second>
    </Bar>
  </ManyBars>
  <Second>Root01_Second</Second>
</Root>
Comments