将XML的子元素序列化为C#属性

时间:2015-01-13 18:56:30

标签: c# xml serialization xsd

我正在尝试在验证XML文档后对其进行反序列化。我已经完成了验证方面的工作,并且已经过适当的验证。我的问题是我有一些子元素,我试图反序列化为一个类。通常,我会创建一个数组来处理子元素,但永远不会有多个子元素(由XSD验证)。因为我没有数组或其他类,所以这些数据似乎没有被反序列化到我的类中。我想,如果我把元素名称放在xmlelement属性中,这会解决它,但没有运气。下面是我的XSD和我的类的片段,其中数据没有被反序列化。子元素存储和状态之外的任何东西都被反序列化了。

<xs:element name="installInfo" >
<xs:complexType>
  <xs:all>
    <xs:element name="softwareProvider" maxOccurs="1" minOccurs="1" >
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="Aurora" />
          <xs:enumeration value="Borealis" />
          <xs:enumeration value="Corona" />
          <xs:enumeration value="Dyson" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="commands" maxOccurs="1" minOccurs="1">
      <xs:simpleType>
        <xs:list>
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="InstallNothing" />
              <xs:enumeration value="InstallStateDatabase" />
              <xs:enumeration value="InstallStateServer" />
              <xs:enumeration value="InstallStoreDatabase" />
              <xs:enumeration value="InstallStoreServer" />
              <xs:enumeration value="InstallMaintenanceProgram" />
              <xs:enumeration value="InstallOther" />
            </xs:restriction>
          </xs:simpleType>
        </xs:list>
      </xs:simpleType>
    </xs:element>
    <xs:element name="new" type="xs:boolean" maxOccurs="1" minOccurs="1" />
    <xs:element name="development" type="xs:boolean" maxOccurs="1" minOccurs="1" />
    <xs:element name="otherUrl" type="xs:string" minOccurs="0" maxOccurs="1" />
    <xs:element name="otherDirectory" type="xs:string" minOccurs="0" maxOccurs="1" />
    <xs:element name="targetAppDirectory" type="xs:string" minOccurs="0" maxOccurs="1" default="C:\Program Files\PullTabs\" />
    <xs:element name ="maintenanceTimer" type="xs:int" minOccurs="0" maxOccurs="1" default="30" />
    <xs:element name="store" maxOccurs="1" minOccurs="0" >
      <xs:complexType>
        <xs:all>
          <xs:element name="storeName" type="xs:string" minOccurs="1" maxOccurs="1" />
          <xs:element name="permitID" type="xs:int" minOccurs="0" maxOccurs="1" />
          <xs:element name="storeProcessName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTabStoreServer" />
          <xs:element name="storeIP" type="xs:string" minOccurs="0" maxOccurs="1" default="192.168.2.2" />
          <xs:element name="storePort" type="xs:int" minOccurs="0" maxOccurs="1" default="19931" />
          <xs:element name="storeDacPacName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTabs.Store.dacpac" />
          <xs:element name="storeDatabaseMame" type="xs:string" minOccurs="0" maxOccurs="1" default="ETickets-StoreServer"/>
          <xs:element name="storeApplicationName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTab.StoreServer" />
          <xs:element name="storeType" minOccurs="0" maxOccurs="1" default="Standard">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="Standard" />
                <xs:enumeration value="Bingo" />
              </xs:restriction>
            </xs:simpleType>
          </xs:element>
        </xs:all>
      </xs:complexType>
    </xs:element>
    <xs:element name="state" maxOccurs="1" minOccurs="0" >
      <xs:complexType>
        <xs:all>
          <xs:element name="stateProcessName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTab.StateServer" />
          <xs:element name="stateIP" type="xs:string" minOccurs="0" maxOccurs="1" default="192.168.2.4" />
          <xs:element name="statePort" type="xs:int" minOccurs="0" maxOccurs="1" default="19932" />
          <xs:element name="stateDacPacName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTabs.State.dacpac" />
          <xs:element name="stateDatabaseName" type="xs:string" minOccurs="0" maxOccurs="1" default="ETickets-StateServer" />
          <xs:element name="stateApplicationName" type="xs:string" minOccurs="0" maxOccurs="1" default="PullTab.StateServer" />
        </xs:all>
      </xs:complexType>
    </xs:element>
  </xs:all>
</xs:complexType>

[XmlRoot("installInfo")]
public class InstallInfo
{
    //public InstallInfo()
    //{
    //    Commands = Operations.InstallNothing;
    //    Development = false;
    //    New = false;
    //    DatabaseManager = new Data.DatabaseManager();
    //    TargetAppDirectory = new DirectoryInfo("C:\\Program Files\\PullTabs\\");
    //    OtherDirectory = "";
    //    OtherURL = "";
    //    MaintenanceTimer = 30;

    //    StoreIP = "192.168.2.2";
    //    StorePort = 19931;
    //    StoreDacPacName = "PullTabs.Store.dacpac";
    //    StoreDatabaseName = "ETickets-StoreServer";
    //    StoreType = StoreTypes.Standard;

    //    try { StoreProcess = ServiceController.GetServices().Where(svc => svc.ServiceName == "PullTabStoreServer").First(); }
    //    catch { }

    //    StateApplicationName = "PullTab.StoreServer";
    //    StateIP = "192.168.2.4";
    //    StatePort = 19932;
    //    StateDacPacName = "PullTabs.State.dacpac";
    //    StateDatabaseName = "ETickets-StateServer";

    //    try { StateProcess = ServiceController.GetServices().Where(svc => svc.ServiceName == "PullTab.StateServer").First(); }
    //    catch { StateProcess = null; }
    //    StateApplicationName = "PullTab.StateServer";
    //}

    //bitwise flag(s) for services to install
    [XmlElement("commands")]
    public Operations Commands { get; set; }

    [XmlElement("development")]
    public bool Development { get; set; }

    [XmlElement("new")]
    public bool New { get; set; }

    [XmlElement("softwareProvider")]
    public PullTab.Data.SoftwareProvider SoftwareProvider { get; set; }

    public string AzureContainer { get { return "pulltabs"; } }

    public DatabaseManager DatabaseManager { get; set; }

    [XmlElement("otherDirectory")]
    public string OtherDirectory { get; set; }

    [XmlElement("otherUrl")]
    public string OtherURL { get; set; }

    //Store Specific Properties
    [XmlElement("StoreName")]
    public string StoreName { get; set; }
    [XmlElement("permitID")]
    public int permitID { get; set; }
    [XmlIgnore]
    public ServiceController StoreProcess { get; set; }

    [XmlElement("storeProcessName")]
    public string storeProcessSerializer
    {
        get
        {
            return StoreProcess.ServiceName.ToString();
        }
        set
        {
            try { StoreProcess = ServiceController.GetServices().Where(svc => svc.ServiceName == value).First(); }
            catch { StoreProcess = null; }
        }

    }

    [XmlElement("storeIP")]
    public string StoreIP { get; set; }
    [XmlElement("storePort")]
    public int StorePort { get; set; }
    [XmlElement("storeDacPacName")]
    public string StoreDacPacName { get; set; }
    [XmlElement("storeDatabaseName")]
    public string StoreDatabaseName { get; set; }
    [XmlElement("storeApplicationName")]
    public string StoreApplicationName { get; set; }
    [XmlElement("storeType")]
    public StoreTypes StoreType { get; set; }

    //State Specific Properties
    [XmlIgnore]
    public ServiceController StateProcess { get; set; }
    [XmlElement("stateProcessName")]
    public string stateProcessSerializer
    {
        get
        {
            return StateProcess.ServiceName.ToString();
        }
        set
        {
            try { StateProcess = ServiceController.GetServices().Where(svc => svc.ServiceName == value).First(); }
            catch { }
        }

    }

    [XmlElement("stateIP")]
    public string StateIP { get; set; }
    [XmlElement("statePort")]
    public int StatePort { get; set; }
    [XmlElement("stateDacPacName")]
    public string StateDacPacName { get; set; }
    [XmlElement("stateDatabaseName")]
    public string StateDatabaseName { get; set; }
    [XmlElement("stateApplicationName")]
    public string StateApplicationName { get; set; }

    [XmlIgnore]
    public DirectoryInfo TargetAppDirectory { get; set; }

    [XmlElement("targetAppDirectory")]
    public string TargetAppDirSerialization
    {
        get
        {
            return TargetAppDirectory.FullName;
        }

        set
        {
            TargetAppDirectory = new DirectoryInfo(value);
        }
    }

    [XmlElement("maintenanceTimer")]
    public int MaintenanceTimer { get; set; }
}

}

0 个答案:

没有答案