1

I have used the following suggestion in order to run export my data in xml through C# code. But my xml format and xsd is different and I am having trouble in generating that.

[Related solution] https://stackoverflow.com/a/6530496/3102147

My xsd file is as follows:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Sam" maxOccurs="unbounded" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Sam">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="UploadCode" type="xs:string" minOccurs="0"/>
                <xs:element name="UploadId" type="xs:string" minOccurs="0"/>
                <xs:element name="Lab" type="xs:string"/>
                <xs:element name="Job" type="xs:string"/>
                <xs:element name="Scheme" type="xs:string"/>
                <xs:element name="SampleType" type="xs:string"/>
                <xs:element name="SampleId" type="xs:string"/>
                <xs:element name="Id" type="xs:string"/>
                <xs:element name="qcOccur" type="xs:byte" minOccurs="0"/>
                <xs:element name="InstrumentName" type="xs:string"/>
                <xs:element name="InstrumentRunWith" type="xs:string"/>
                <xs:element name="AnalDate" type="xs:string" minOccurs="0"/>
                <xs:element name="AnalTime" type="xs:string" minOccurs="0"/>
                <xs:element name="Analyst" type="xs:string" minOccurs="0"/>
                <xs:element name="ResultMode" type="xs:string"/>
                <xs:element name="Dilution" type="xs:string" minOccurs="0"/>            
                <xs:element ref="Analyses"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Analyses">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Ana" maxOccurs="unbounded" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Ana">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Analysis" type="xs:string"/>
                <xs:element name="Parameter" type="xs:string" minOccurs="0"/>
                <xs:element name="Result" type="xs:string"/>
                <xs:element name="AnalDate" type="xs:string"/>
                <xs:element name="AnalTime" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

My xml output file should be as follows:

<list>
  <Sam>
    <Lab>ZAORK_M</Lab>
    <Job>ORK20-01536</Job>
    <Scheme>GO_FAG50V</Scheme>
    <SampleType>Unknown</SampleType>
    <SampleId>ORK20-01536.001</SampleId>
    <Id>ORK_S0003725727</Id>
    <InstrumentName>B0001_OFFLINE_WEIGHT</InstrumentName>
    <InstrumentRunWith>MKY386</InstrumentRunWith>
    <AnalDate>3/10/2019|dd/MM/yyyy</AnalDate>
    <AnalTime>8:33:23 AM|hh:mm:ss a</AnalTime>
    <Analyst>PHILLIP_HARRIS</Analyst>
    <ResultMode>WEIGHT_VOLUME</ResultMode>
    <Analyses>
      <Ana>
        <Analysis>Weight</Analysis>
        <Result>0.1111</Result>
        <AnalDate>3/10/2019|dd/MM/yyyy</AnalDate>
        <AnalTime>8:33:23 AM|hh:mm:ss a</AnalTime>
      </Ana>
      <Ana>
        <Analysis>Volume</Analysis>
        <Result>0.1</Result>
        <AnalDate>3/10/2019|dd/MM/yyyy</AnalDate>
        <AnalTime>8:33:23 AM|hh:mm:ss a</AnalTime>
      </Ana>
    </Analyses>
  </Sam>
</list>

My code is as follows:

var Sample = new Sam { UploadCode = "test1", UploadId = "test2" };
            var Analysis = new Ana { Analysis = "Test2", Result = "123" };

            var serializer1 = new XmlSerializer(typeof(Sam));
            using (var stream = new StreamWriter("C:\\test.xml"))
            {
                serializer1.Serialize(stream, Sample);
            }

The problem I am having is that I cannot create list node before sam and also not able to put Analysis under sam and Ana under Analysis.

I am not able to understand how can I fix my code to so that it knows properly which child node is how to apply to correct parent. Please help if anyone knows how to fix this.

1 Answers1

1

You are missing some steps in your code to actually make the message. The generated cs file from your xsd has a class called list(). That is your start point when you want to create the complete xml structure. Under list you have a repeating group called Sam, xsd.exe translated that into an array. Xsd.exe does not know lists. so the next thing you need to do is define the occurs that Sam will be in the xml file. And for eacht Sam there is an array called Analysis that also needs to be defined. If you do this and then serialize the content to an xml file you will get what you want. But keep in mind that you need to fill all the elements in the classes. If you do not the serializer will take the default value, which is 0 for numeric fields and null for alphanumeric. an null value is not serialized so the element is left out making your xml content invalid.

the below example works but is missing a lot of elements

var list = new list();
list.Sam = new Sam[1];
            
var sample = new Sam { UploadCode = "test1", UploadId = "test2", Analyses = new Ana[1]};
var analysis = new Ana { Analysis = "Test2", Result = "123" };

sample.Analyses[0] = analysis;
list.Sam[0] = sample;

var serializer1 = new XmlSerializer(typeof(list));
using (var stream = new StreamWriter("test.xml"))
{
   serializer1.Serialize(stream, list);
}
martijn
  • 485
  • 2
  • 9
  • Awesome thanks it works like charm. I have one more question though, is it possible to loop through all nodes and set their value dynamically in the loop ? If yes, can you please advise how ? – percydeveloper Oct 14 '20 at 05:46
  • I'm not sure what you actually mean with this but if you want to fill the elements with data i expect that you need some kind of mapping routine. you need to loop trough your source to fill the list. – martijn Oct 14 '20 at 06:01
  • No worries, I got it. Thanks for your help much appreciated – percydeveloper Oct 14 '20 at 06:03