使用XSD生成的类

时间:2017-04-24 18:33:12

标签: c# xml xsd xml-deserialization

我知道这已被问过几次,但我花了几个小时查看帖子而没有找到答案。我有一个使用XSD工具生成的类。我可以毫无问题地序列化和序列化完整的XML,我遇到的问题是当我想将XML的一部分序列化到相关的类时。

我有以下XML

<?xml version="1.0" encoding="utf-8"?>
  <!--This is the party that has placed the order-->
  <Address addressIDDomain="" isoCountryCode="NO" addressID="Optional - Visible Industry customer number">
    <Name>Name max chars 50</Name>
    <Company>Company max chars 50</Company>
    <PostalAddress>
      <Street>Max 50 chars</Street>
      <City>Max 20 chars</City>
      <PostalCode>Max10Chars</PostalCode>
      <MailingAddress>Max 50 chars</MailingAddress>
      <State></State>
      <Country></Country>
    </PostalAddress>
    <Email>Email address</Email>
    <Telephone>99999999</Telephone>
  </Address>

其中有一个由XSD生成的相应类

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.visibleindustry.com/automation/standardorder/StandardOrder.xsd")]
public partial class OrderAddress : object, System.ComponentModel.INotifyPropertyChanged {

    private OrderAddressAddress addressField;

    /// <remarks/>
    public OrderAddressAddress Address {
        get {
            return this.addressField;
        }
        set {
            this.addressField = value;
            this.RaisePropertyChanged("Address");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.visibleindustry.com/automation/standardorder/StandardOrder.xsd")]
public partial class OrderAddressAddress : object, System.ComponentModel.INotifyPropertyChanged {

    private string nameField;

    private string companyField;

    private PostalAddress postalAddressField;

    private string emailField;

    private string telephoneField;

    private string isoCountryCodeField;

    private string addressIDField;

    private string addressIDDomainField;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
            this.RaisePropertyChanged("Name");
        }
    }

    /// <remarks/>
    public string Company {
        get {
            return this.companyField;
        }
        set {
            this.companyField = value;
            this.RaisePropertyChanged("Company");
        }
    }

    /// <remarks/>
    public PostalAddress PostalAddress {
        get {
            return this.postalAddressField;
        }
        set {
            this.postalAddressField = value;
            this.RaisePropertyChanged("PostalAddress");
        }
    }

    /// <remarks/>
    public string Email {
        get {
            return this.emailField;
        }
        set {
            this.emailField = value;
            this.RaisePropertyChanged("Email");
        }
    }

    /// <remarks/>
    public string Telephone {
        get {
            return this.telephoneField;
        }
        set {
            this.telephoneField = value;
            this.RaisePropertyChanged("Telephone");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string isoCountryCode {
        get {
            return this.isoCountryCodeField;
        }
        set {
            this.isoCountryCodeField = value;
            this.RaisePropertyChanged("isoCountryCode");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string addressID {
        get {
            return this.addressIDField;
        }
        set {
            this.addressIDField = value;
            this.RaisePropertyChanged("addressID");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string addressIDDomain {
        get {
            return this.addressIDDomainField;
        }
        set {
            this.addressIDDomainField = value;
            this.RaisePropertyChanged("addressIDDomain");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

当我尝试将XML序列化到Address类时,我能够填充地址参数字段,但所有其他成员变量都为空。

我的最新代码在

之下
                XmlDocument xml = new XmlDocument();
            xml.Load("OrderAddress.xml");

            StandardOrder.OrderAddress oa = new StandardOrder.OrderAddress();

            XmlRootAttribute xRoot = new XmlRootAttribute("Address");
            xRoot.IsNullable = false;

            XmlSerializer xs = new XmlSerializer(typeof(StandardOrder.OrderAddress), xRoot);
            XmlReader xr = XmlReader.Create(new StringReader(xml.InnerXml));
            oa = (StandardOrder.OrderAddress)xs.Deserialize(xr);

我尝试了各种相同的结果。

我希望能够序列化主类的任何子类,因此每次其中一个子类需要更新或显示时,我不必在程序中传递完整的XML类。

由于

0 个答案:

没有答案