如何将xml数据绑定到文本框

时间:2018-11-10 02:09:46

标签: c# asp.net xml

我试图根据下拉列表中选择的值将xml文档中的数据绑定到文本框。 !Page.IsPostBack的默认值是xml文件的第一个节点。我不确定如何将xml数据的数据绑定到文本框。任何帮助将不胜感激。

XML:registrations.xml:

<?xml version="1.0" encoding="UTF-8"?>
<RegistrationCollection>
   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>k.bernal@gmail.com</emailAddress>
         <registrationType>Conference Only</registrationType>
         <attendingSocialEvent>Yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>Conference and Dinner</registrationType>
         <attendingSocialEvent>No</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>Conference Only</registrationType>
         <attendingSocialEvent>Yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</emailAddress>
         <registrationType>Conference and Dinner</registrationType>
         <attendingSocialEvent>No</attendingSocialEvent>
      </Registration>
   </registrations>
</RegistrationCollection>

ASP.NET背后的代码:edit.aspx.cs:

public partial class edit : System.Web.UI.Page
{
const string FILENAME = "registrations.xml";
string file = "";

protected void Page_Load(object sender, EventArgs e)
{

    file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME);

    if (!Page.IsPostBack)
    {
        bindXmlForm();
    }
}

protected void bindXmlForm()
{
    //Code to deserialize and populate dropdownlist ddlIDEdit
  XmlSerializer xmlSerializer = new XmlSerializer(typeof(RegistrationCollection));
    FileStream fileStream = new FileStream(file, FileMode.Open);

    RegistrationCollection registrationCollection = (RegistrationCollection)xmlSerializer.Deserialize(fileStream);
    fileStream.Close();

    ddlIdEdit.DataSource = registrationCollection.registrations;

    ddlIdEdit.DataValueField = "id";
    ddlIdEdit.DataTextField = "id";
    ddlIdEdit.DataBind();

    txtFullNameEdit.Text = Registration.fullName;//error happens here object reference required.

}

我不确定要使用什么来创建对象引用。 foreach无法正常工作。

0 个答案:

没有答案