简体   繁体   中英

Problem deserializing xml file

I auto generated an xsd file from the below xml and used xsd2code to get ac# class. The problem is the entire xml doesn't deserialize.

Here is how I'm attempting to deserialize:

    static void Main(string[] args)
    {
        using (TextReader textReader = new StreamReader("config.xml"))
        {
           // string temp = textReader.ReadToEnd();
            XmlSerializer deserializer = new XmlSerializer(typeof(project));
            project p = (project)deserializer.Deserialize(textReader);
        }
    }

here is the actual XML:

<?xml version='1.0' encoding='UTF-8'?>
<project>
  <scm class="hudson.scm.SubversionSCM">
    <locations>
      <hudson.scm.SubversionSCM_-ModuleLocation>
        <remote>https://svn.xxx.com/test/Validation/CPS DRTest DLL/trunk</remote>
      </hudson.scm.SubversionSCM_-ModuleLocation>
    </locations>
    <useUpdate>false</useUpdate>
    <browser class="hudson.scm.browsers.FishEyeSVN">
      <url>http://fisheye.xxxx.net/browse/Test/</url>
      <rootModule>Test</rootModule>
    </browser>
    <excludedCommitMessages></excludedCommitMessages>
  </scm>
  <openf>Hello there</openf>
  <buildWrappers/>
</project>

When I run the above, the locations node remains null.

Here is the xsd that I'm using:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="project">
    <xs:complexType>
      <xs:all>
        <xs:element name="openf" type="xs:string" minOccurs="0" />
        <xs:element name="buildWrappers" type="xs:string" minOccurs="0" />
        <xs:element name="scm" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="useUpdate" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
              <xs:element name="excludedCommitMessages" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
              <xs:element name="locations" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="hudson.scm.SubversionSCM_-ModuleLocation" minOccurs="0">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="remote" type="xs:string" minOccurs="0" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="browser" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="url" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
                    <xs:element name="rootModule" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
                  </xs:sequence>
                  <xs:attribute name="class" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="class" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:all>
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="project" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Figured it out. When using xsd2code I had select options to target the 3.5 framework and include the xml attributes. It now deserializes as expected. Not sure which one did it, but works now.

According to this page '.' is not a valid XML tag name character, so you need to rename <hudson.scm.SubversionSCM_-ModuleLocation> to something without the dots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM