-1

I want to deserialize XML to a list and simple string:

<GeneralTranslation>
    <Day>Day</Day>
    <Month>Month</Month>
    <Year>Year</Year>
    <Submit>Submit</Submit>
    <Select>Please select</Select>

    <TradingExp>
        <Option>Less than a year</Option>
        <Option>1-2 years</Option>
        <Option>3-5 years</Option>
        <Option>Above 5 years</Option>
    </TradingExp>

    <LevelOfInvestment>
        <Option>Less than 5K</Option>
        <Option>Between 5K and 15K</Option>
        <Option>Between 15K and 50K</Option>
        <Option>Above 50KK</Option>
    </LevelOfInvestment>
</GeneralTranslation>

into this: (is there anyway to do it into list without doing it manually?)

[XmlRoot("GeneralTranslation")]
public class GeneralTrnaslation
{
    public string Day { get; set; }
    public string Month { get; set; }
    public string Year { get; set; }
    public string Above { get; set; }
    public string Select { get; set; }
    public string Submit { get; set; }
    public List<string> LevelOfInvestment { get; set; }
    public List<string> TradingExp { get; set; } 
}

this is what i came up with, all works except the list:

(GeneralTrnaslation)serializer.Deserialize(streamTrans)
4

2 回答 2

1

I have tried to generated a class from your xml. At the beginning I have generated an xsd from your xml

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="GeneralTranslation">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Day" type="xs:string" />
        <xs:element name="Month" type="xs:string" />
        <xs:element name="Year" type="xs:string" />
        <xs:element name="Submit" type="xs:string" />
        <xs:element name="Select" type="xs:string" />
        <xs:element name="TradingExp">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="Option" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="LevelOfInvestment">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="Option" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

then using xsd utility I have generated the following class

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class GeneralTranslation {

        private string dayField;

        private string monthField;

        private string yearField;

        private string submitField;

        private string selectField;

        private string[] tradingExpField;

        private string[] levelOfInvestmentField;

        /// <remarks/>
        public string Day {
            get {
                return this.dayField;
            }
            set {
                this.dayField = value;
            }
        }

        /// <remarks/>
        public string Month {
            get {
                return this.monthField;
            }
            set {
                this.monthField = value;
            }
        }

        /// <remarks/>
        public string Year {
            get {
                return this.yearField;
            }
            set {
                this.yearField = value;
            }
        }

        /// <remarks/>
        public string Submit {
            get {
                return this.submitField;
            }
            set {
                this.submitField = value;
            }
        }

        /// <remarks/>
        public string Select {
            get {
                return this.selectField;
            }
            set {
                this.selectField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Option", IsNullable=false)]
        public string[] TradingExp {
            get {
                return this.tradingExpField;
            }
            set {
                this.tradingExpField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Option", IsNullable=false)]
        public string[] LevelOfInvestment {
            get {
                return this.levelOfInvestmentField;
            }
            set {
                this.levelOfInvestmentField = value;
            }
        }
    }
}

so out of here you can get string arrays, but not list

below there is the the how-to:

  1. Open your XML in Visual Studio
  2. Select the tab with your XML. In main menu select XML/Create Schema. (the studio will open you the scheme file)
  3. use xsd.exe util. (http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx)

in some cases I am doing the following trick with xsd's:

when I know that my document schmema can be changed during development cycle:

I am adding XSD file into my project. In project file I am adding the following sections

<PropertyGroup>
    <WIN_SDK_PATH>$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows@CurrentInstallFolder)</WIN_SDK_PATH>
    <WIN_SDK_BIN_PATH>$(WIN_SDK_PATH)bin</WIN_SDK_BIN_PATH>
    <XSDUTIL>"$(WIN_SDK_BIN_PATH)\xsd.exe"</XSDUTIL>
  </PropertyGroup>
  <Target Name="XSD2CS">
    <Exec Command="$(XSDUTIL) &quot;$(ProjectDir)XMLSchema1.xsd&quot; /classes /out:&quot;$(ProjectDir)obj&quot; /namespace:$(RootNamespace).XSD" />
  </Target>
  <Target Name="BeforeBuild" DependsOnTargets="XSD2CS">
  </Target>

and one more section in the that contain my project files

<Compile Include="$(ProjectDir)obj\XMLSchema1.cs">
  <SubType>Code</SubType>
</Compile>

each time when I am compiling my project I am getting a class that corresponds the latest actual version of my xsd. so all I need to take care of is a correct and full xsd file.

于 2013-11-11T15:49:57.893 回答
1

Assuming you are trying a standard method of deserialization, the problem is that the XML does not have a standard list of strings, but a list of 'Option'.

If it were using standard .NET lists of strings, the results would look like:

 <LevelOfInvestment>
    <string>test1</string>
    <string>test2</string>
 </LevelOfInvestment>
于 2013-11-11T15:51:15.383 回答