Question

I have a xml file that I want to read it and then interpret and convert to respective classes that were generated from xsd tool. Here are the steps which I am trying:

  1. I created a XSD file.
  2. Converted the respective xsd file into respective set of classes.
  3. I made some xml files through same set of classes (from step 2) and xmlserializer.
  4. Now I am reading back from those xml files and I wan't to convert it into classes (generated from step2)

I am pasting the code I have worked till now, I seem to get an exception when I am deserializing, {"There is an error in XML document (0, 0)."}

            var doc = XDocument.Load(filePath);

        var query2 = from b in doc.Root.Descendants()
                     select b;

        foreach (var item in query2)
        {
            switch (item.Name.LocalName)
            {
                case "SomeStringValue": 
                    XmlSerializer srz = new XmlSerializer(typeof(SomeClassGeneratedfromXSD));
                    var writer=item.CreateReader();
                    parameterFromFile.SomeProperty = (SomeClassGeneratedfromXSD)srz.Deserialize(writer);
                    //srz.Deserialize(item);
                    break;

I am pasting a snippet of what my xsd looked like:

<xs:complexType name="Parameters">
<xs:all>
  <xs:element name="A">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="mstns:Restricted8CharString">
          <xs:attribute name="Caption" use="required" fixed="Caption for A">
            <xs:simpleType>
              <xs:restriction base="xs:string"></xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="ActionWhenMaxReached" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:short">
                <xs:pattern value="[1-3]"></xs:pattern>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="Expression" type="xs:string" default="0" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  <xs:element name="B">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:short">
          <xs:attribute name="Caption" use="prohibited">
            <xs:simpleType>
              <xs:restriction base="xs:string"></xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="ActionWhenMaxReached" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:short">
                <xs:pattern value="[1-3]"></xs:pattern>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="Expression" type="xs:string" default="0" />
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
  </xs:all>
  </xs:complexType>
Was it helpful?

Solution 2

After struggling, I have found out the solution:

Actually I was trying to parse an inner element, which was incorrect; I was already having the classes from the xsd tool, so I can Simply use the code as:

XmlSerializer serializer = new XmlSerializer(typeof(RootClass));
        using (TextReader reader = new StreamReader(filePath))
        {
            RootClass parameterFromFile = (RootClass)serializer.Deserialize(reader);
        }

OTHER TIPS

Your closing tag is incorrect. It should be:

</xs:complexType>

and not

</xs:complexType name="Parameters">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top