简体   繁体   English

尝试将xml文件解析为C#aspx.net中的对象

[英]Trying to parse an xml file into an Object in C# aspx.net

I have a problem trying to serialize an XML file into an object. 我在尝试将XML文件序列化为对象时遇到问题。 I got this to work in an other project, but this time it does not work. 我让它在另一个项目中工作,但是这次不起作用。 The object and its elements are always empty. 对象及其元素始终为空。

My XML file: 我的XML文件:

  <ConferenceTextAttributes>
  <_lbl_TitleOfPage_Text>Registration</_lbl_TitleOfPage_Text>
  <_lbl_textBoxInfo_Text>(* mandatory fields)</_lbl_textBoxInfo_Text>
  <_lbl_mrsmr_Text>Mr/Ms:*</_lbl_mrsmr_Text>
  <_txt_mrsmr_Text></_txt_mrsmr_Text>
  <_lbl_title_Text>Title:</_lbl_title_Text>
  <_txt_title_Text></_txt_title_Text>
  <_lbl_firstname_Text>First Name:*</_lbl_firstname_Text>
  <_txt_firstname_Text></_txt_firstname_Text>
  <_lbl_surname_Text>Surname:*</_lbl_surname_Text>
  <_txt_surname_Text></_txt_surname_Text>
  <_lbl_institution_Text>Institution:*</_lbl_institution_Text>
  <_txt_institution_Text></_txt_institution_Text>
  <_lbl_department_Text>Department:*</_lbl_department_Text>
  <_txt_department_text></_txt_department_text>
  <_lbl_address_Text>Address:*</_lbl_address_Text>
  <_txt_address_text></_txt_address_text>
  <_lbl_zip_Text>Zip Code:*</_lbl_zip_Text>
  <_txt_zip_Text></_txt_zip_Text>
  <_lbl_city_Text>City:*</_lbl_city_Text>
  <_txt_city_Text></_txt_city_Text>
  <_lbl_country_Text>Country:*</_lbl_country_Text>
  <_txt_country_Text></_txt_country_Text>
  <_lbl_phone_Text>Phone:*</_lbl_phone_Text>
  <_txt_phone_Text></_txt_phone_Text>
  <_lbl_fax_Text>Fax:</_lbl_fax_Text>
  <_txt_fax_Text></_txt_fax_Text>
  <_lbl_email_Text>E-mail:*</_lbl_email_Text>
  <_txt_email_Text></_txt_email_Text>
  <_lbl_info_Text>I participate as a (former) mebmer of:</_lbl_info_Text>
  <_lbl_dropdown1_Text>If you are (former) member of NGFN-Plus / NGFN-Transfer please select</_lbl_dropdown1_Text>
  <_lbl_other_Text>other:</_lbl_other_Text>
  <_txt_other_Text></_txt_other_Text>
  <_lbl_chbInfo_Text>I will participate on these days:</_lbl_chbInfo_Text>
  <_chkb_day01_text>Montag</_chkb_day01_text>
  <_chkb_day02_text>Dienstag</_chkb_day02_text>
  <_chkb_day03_text>Mittwoch</_chkb_day03_text>
  <_chkb_day04_text>Donnerstag</_chkb_day04_text>
  <_chkb_day05_text>Freitag</_chkb_day05_text>
  <_chkb_day06_text>Samstag</_chkb_day06_text>
  <_chkb_day07_text>Sonntag</_chkb_day07_text>
  <_b_submit_Text>Register!</_b_submit_Text>
  <_b_cancle_Text>Cancel!</_b_cancle_Text>
  </ConferenceTextAttributes>

My class: 我的课:

public class ConferenceTextAttributes
{
    //Registration
    public string lbl_TitleOfPage_Text = string.Empty;
    public string lbl_textBoxInfo_Text = string.Empty;
    public string lbl_mrsmr_Text = string.Empty;
    public string txt_mrsmr_Text = string.Empty;
    public string lbl_title_text = string.Empty;
    public string txt_title_Text = string.Empty;
    public string lbl_firstname_Text = string.Empty;
    public string txt_firstname_Text = string.Empty;
    public string lbl_surname_Text = string.Empty;
    public string txt_surname_Text = string.Empty;
    public string lbl_institution_Text = string.Empty;
    public string txt_institution_Text = string.Empty;
    public string lbl_department_Text = string.Empty;
    public string txt_department_text = string.Empty;
    public string lbl_address_Text = string.Empty;
    public string txt_address_text = string.Empty;
    public string lbl_zip_Text = string.Empty;
    public string txt_zip_Text = string.Empty;
    public string lbl_city_Text = string.Empty;
    public string txt_city_Text = string.Empty;
    public string lbl_country_Text = string.Empty;
    public string txt_country_Text = string.Empty;
    public string lbl_phone_Text = string.Empty;
    public string txt_phone_Text = string.Empty;
    public string lbl_fax_Text = string.Empty;
    public string txt_fax_Text = string.Empty;
    public string lbl_email_Text = string.Empty;
    public string txt_email_Text = string.Empty;
    public string lbl_info_Text = string.Empty;
    public string lbl_dropdown1_Text = string.Empty;
    public string lbl_other_Text = string.Empty;
    public string txt_other_Text = string.Empty;
    public string lbl_chbInfo_Text = string.Empty;
    public string chkb_day01_text = string.Empty;
    public string chkb_day02_text = string.Empty;
    public string chkb_day03_text = string.Empty;
    public string chkb_day04_text = string.Empty;
    public string chkb_day05_text = string.Empty;
    public string chkb_day06_text = string.Empty;
    public string chkb_day07_text = string.Empty;
    public string b_submit_Text = string.Empty;
    public string b_cancle_Text = string.Empty;

    public ConferenceTextAttributes(string _lbl_TitleOfPage_Text,string _lbl_textBoxInfo_Text, string _lbl_mrsmr_Text, string _txt_mrsmr_Text, string _lbl_title_text, string _txt_title_Text, string _lbl_firstname_Text, string _txt_firstname_Text, string _lbl_surname_Text, string _txt_surname_Text, string _lbl_institution_Text, string _txt_institution_Text, string _lbl_department_Text, string _txt_department_text, string _lbl_address_Text, string _txt_address_text, string _lbl_zip_Text, string _txt_zip_Text, string _lbl_city_Text, string _txt_city_Text, string _lbl_country_Text, string _txt_country_Text, string _lbl_phone_Text, string _txt_phone_Text, string _lbl_fax_Text, string _txt_fax_Text, string _lbl_email_Text, string _txt_email_Text, string _lbl_info_Text, string _lbl_dropdown1_Text, string _lbl_other_Text, string _txt_other_Text, string _lbl_chbInfo_Text, string _chkb_day01_text, string _chkb_day02_text, string _chkb_day03_text, string _chkb_day04_text, string _chkb_day05_text, string _chkb_day06_text, string _chkb_day07_text, string _b_submit_Text, string _b_cancle_Text)
    {
        lbl_TitleOfPage_Text = _lbl_TitleOfPage_Text;
        lbl_textBoxInfo_Text = _lbl_textBoxInfo_Text;
        lbl_mrsmr_Text = _lbl_mrsmr_Text;
        txt_mrsmr_Text = _txt_mrsmr_Text;
        lbl_title_text = _lbl_title_text;
        txt_title_Text = _txt_title_Text;
        lbl_firstname_Text = _lbl_firstname_Text;
        txt_firstname_Text = _txt_firstname_Text;
        lbl_surname_Text = _lbl_surname_Text;
        txt_surname_Text = _txt_surname_Text;
        lbl_institution_Text = _lbl_institution_Text;
        txt_institution_Text = _txt_institution_Text;
        lbl_department_Text = _lbl_department_Text;
        txt_department_text = _txt_department_text;
        lbl_address_Text = _lbl_address_Text;
        txt_address_text = _txt_address_text;
        lbl_zip_Text = _lbl_zip_Text;
        txt_zip_Text = _txt_zip_Text;
        lbl_city_Text = _lbl_city_Text;
        txt_city_Text = _txt_city_Text;
        lbl_country_Text = _lbl_country_Text;
        txt_country_Text = _txt_country_Text;
        lbl_phone_Text = _lbl_phone_Text;
        txt_phone_Text = _txt_phone_Text;
        lbl_fax_Text = _lbl_fax_Text;
        txt_fax_Text = _txt_fax_Text;
        lbl_email_Text = _lbl_email_Text;
        txt_email_Text = _txt_email_Text;
        lbl_info_Text = _lbl_info_Text;
        lbl_dropdown1_Text = _lbl_dropdown1_Text;
        lbl_other_Text = _lbl_other_Text;
        txt_other_Text = _txt_other_Text;
        lbl_chbInfo_Text = _lbl_chbInfo_Text;
        chkb_day01_text = _chkb_day01_text;
        chkb_day02_text = _chkb_day02_text;
        chkb_day03_text = _chkb_day03_text;
        chkb_day04_text = _chkb_day04_text;
        chkb_day05_text = _chkb_day05_text;
        chkb_day06_text = _chkb_day06_text;
        chkb_day07_text = _chkb_day07_text;
        b_submit_Text = _b_submit_Text;
        b_cancle_Text = _b_cancle_Text;
    }

     public ConferenceTextAttributes()
     {

     }
}

My method to serialize: 我的序列化方法:

public ConferenceTextAttributes LeseMeistenTextAttribute(string sPfad2)
{
    XmlSerializer ser2 = new XmlSerializer(typeof(ConferenceTextAttributes));
    StreamReader sr2 = new StreamReader(sPfad2);
    ConferenceTextAttributes MyTextAttributes = 
        (ConferenceTextAttributes)ser2.Deserialize(sr2);
    sr2.Close();
    return MyTextAttributes;
}

There are extra underscores in the field names in the xml. xml的字段名称中有额外的下划线。 Use XmlElement attributes in you class to specify those. 在您的课程中使用XmlElement属性来指定这些属性。

[XmlElement("_lbl_msmr_text")]
public string lbl_msmr_text { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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