xml和中继器

时间:2011-03-04 19:51:26

标签: c# asp.net xml

我是否必须使用XPath链接我的XML文档 到转发器还是有另一种方式。

这是我的一些代码。

第一页循环遍历数据并以XML格式存储:

public virtual void Button_Click(object sender, EventArgs args)
          {

             TableControl recControls = (TableControl)this.Page.FindControlRecursively("TableControl");
              if (recControls != null)
              {
                  TableControlRow[] rows = recControls.GetSelectedRecordControls();
                  this.Page.Session["RegistrationTableControl"] = rows;

                  // create XML writer to 
                  using(System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(@"c:\temp\reg.xml"))
                  {
                      XmlWriterSettings settings = new XmlWriterSettings();
                      settings.OmitXmlDeclaration = true;
                      settings.ConformanceLevel = ConformanceLevel.Fragment;
                      settings.CloseOutput = false;



                      writer.WriteStartDocument();
                      writer.WriteStartElement("SubContractors");
                      foreach(TableControlRow rec in rows)
                      {

                          writer.WriteElementString("SubContrator1", rec.BUSINESSNAME.Text);
                          writer.WriteElementString("SubContactName1", rec.OWNERNAME.Text);
                          writer.WriteElementString("SubAddress1", rec.ADDRESS.Text);
                          writer.WriteElementString("SubCity1", rec.CITY.Text);
                          writer.WriteElementString("SubState1", rec.STATE.Text);
                          writer.WriteElementString("SubZipCode1", rec.ZIPCODE.Text);
                          writer.WriteElementString("SubPhone1", rec.PHONE.Text);
                          writer.WriteElementString("SubFax1", rec.FAX.Text);
                          writer.WriteElementString("SubEmail1", rec.EMAIL.Text);
                          writer.WriteElementString("Status1", "Please enter information");
                          writer.WriteElementString("Referral", "1");

                      }
                      writer.WriteEndElement();
                      writer.WriteEndDocument();
                 }


              }}

创建我的xml文档。

代码:我需要在新创建的XML文档中获取数据 使用此代码填充我的表控制转发器。

第2页

public override void DataBind()
    {
        base.DataBind();


        // Make sure that the DataSource is initialized.
        if (this.DataSource == null)
        {
            return;
        }
         // Setup the pagination controls.
        BindPaginationControls();

        // Populate all filters data.

        // Bind the repeater with the list of records to expand the UI.
        System.Web.UI.WebControls.Repeater rep = (System.Web.UI.WebControls.Repeater)(this.FindControl("TableControlRepeater"));
        rep.DataSource = this.DataSource;
        rep.DataBind();

        int index = 0;
        foreach (System.Web.UI.WebControls.RepeaterItem repItem in rep.Items)
        {

            // Make sure that the DataSource is initialized.


            // Loop through all rows in the table, set its DataSource and call DataBind().
            TableControlRow recControl = (TableControlRow)(repItem.FindControl("TableControlRow"));
            recControl.DataSource = this.DataSource[index];
            recControl.DataBind();
            recControl.Visible = !this.InDeletedRecordIds(recControl);
            index += 1;
        }
    }

我已将XML数据放在第2页中,成为转发器的数据源。

任何帮助或建议都非常有用。我真的不需要重写代码。

2 个答案:

答案 0 :(得分:1)

LINQ-To-XML可能是完成此任务的最简单方法。请参阅Scott Gu的post

伪码:

var xml = XDocument.Load(...);
reapeater.DataSource = from x in xml.Descendents("z") 
    where x.Attributes("y").Equals("select value") 
    select new { Value1 = x.Element("Value1"), Value2 = x.Element("Value2") };
repeater.DataBind();

答案 1 :(得分:0)

我使用LINQ to XML

用于查询XML文档更容易。