将XmlDataSource动态绑定到上载的XML

时间:2017-05-10 15:01:36

标签: c# asp.net xml binding xmldatasource

XmlDataSource绑定到XmlDocument

我想做什么:

从用户读取XML文件并将其显示在GridView和DetailsView中。在这里,用户可以进行修改或添加新条目。然后他可以将修改后的XML文件下载到他的硬盘中。

示例XML:

<?xml version="1.0" encoding="utf-8"?>
<Mapping Version="2">
  <MappingRelation Active="True" DirectionVar1ToVar2="True" Offset="0" Factor="1" Assignment="OnChange">
    <ValueObject1 ItemType="symSystemVariable">
      <BusType>-1</BusType>
      <DatabaseName>HumiditySensor::p_Ambient</DatabaseName>
      <Description />
      <EnvVarName />
      <FullName>HumiditySensor::p_Ambient</FullName>
      <ID>1</ID>
      <MessageName />
      <Name>HumiditySensor::p_Ambient</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName />
      <NodeName />
      <SignalName />
      <VariableType>2</VariableType>
    </ValueObject1>
    <ValueObject2 ItemType="symSignal">
      <BusType>1</BusType>
      <DatabaseName>CAN1</DatabaseName>
      <Description />
      <EnvVarName>ECAN 1</EnvVarName>
      <FullName>CAN1::HumiditySensor::Luftfeuchte_HDT2800::_Ambient_Pressure</FullName>
      <ID>0</ID>
      <MessageName>Luftfeuchte_HDT2800</MessageName>
      <Name>_Ambient_Pressure</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName>ECAN 1</NetworkName>
      <NodeName>HumiditySensor</NodeName>
      <SignalName>_Ambient_Pressure</SignalName>
      <VariableType>0</VariableType>
    </ValueObject2>
  </MappingRelation>
  <MappingRelation Active="True" DirectionVar1ToVar2="True" Offset="0" Factor="1" Assignment="OnChange">
    <ValueObject1 ItemType="symSystemVariable">
      <BusType>-1</BusType>
      <DatabaseName>HumiditySensor::spec_Humidity</DatabaseName>
      <Description />
      <EnvVarName />
      <FullName>HumiditySensor::spec_Humidity</FullName>
      <ID>1</ID>
      <MessageName />
      <Name>HumiditySensor::spec_Humidity</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName />
      <NodeName />
      <SignalName />
      <VariableType>2</VariableType>
    </ValueObject1>
    <ValueObject2 ItemType="symSignal">
      <BusType>1</BusType>
      <DatabaseName>CAN1</DatabaseName>
      <Description />
      <EnvVarName>ECAN 1</EnvVarName>
      <FullName>CAN1::HumiditySensor::Luftfeuchte_HDT2800::_Specific_Humidity</FullName>
      <ID>0</ID>
      <MessageName>Luftfeuchte_HDT2800</MessageName>
      <Name>_Specific_Humidity</Name>
      <NeedsMessage>False</NeedsMessage>
      <NetworkName>ECAN 1</NetworkName>
      <NodeName>HumiditySensor</NodeName>
      <SignalName>_Specific_Humidity</SignalName>
      <VariableType>0</VariableType>
    </ValueObject2>
  </MappingRelation>
</Mapping>

到目前为止我在做什么:

我将提供的XML文档读入XmlDocument。这个Xmldocument绑定到XmlDataSource(id =&#34; id_DataSourceMP&#34;),后者又绑定到GridView(id =&#34; id_GridViewMP&#34;)和DetailsView(id =&#34; id_DetailsViewMP& #34;)控制。 在这个函数里面,我可以读回XmlDataSource中的数据,并且可以改变例如DetailsView的PageIndex。

但是在其他函数(mp_rowSelected,id_MapButton_Click)中,这会产生错误&#34;对象引用未设置为对象的实例&#34;。

这是背后的代码:

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.XPath;



public partial class Default : System.Web.UI.Page
{
    //-----------------------------------------------------------------------
    //PageCallBacks
    //-----------------------------------------------------------------------

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //DataTable dt = new DataTable();
            //Session["DataTable"] = dt;

            //XmlDocument xmlMap = new XmlDocument();
            //Session["Mapping"] = xmlMap;

            //TreeNode sv_selectedNode = new TreeNode();
            //TreeNode pv_selectedNode = new TreeNode();
        }

    }

    //--------------------------------------------------------------------------------------
    //Callback Functions
    //--------------------------------------------------------------------------------------

    protected void sv_loadXml(Object sender, EventArgs e)
    {
    }

    protected void pv_loadXml(Object sender, EventArgs e)
    { 
    }

    protected void tree_nodechange(Object sender, EventArgs e)
    {
    }

    protected void mp_loadXml(Object sender, EventArgs e)
    {
        //String myFile = Server.MapPath("~/data/HumiditySensor_Map.xml");
        if (id_FileUploaderMP.HasFile)
        {
            Stream myStream = id_FileUploaderMP.FileContent;
            StreamReader sr = new StreamReader(myStream);

            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(sr);

            if (myDoc.DocumentElement.Name != "Mapping")
            {
                id_writeOut.InnerText = "Laden fehlgeschlagen!";
                return;
            }

            id_DataSourceMP.Data = myDoc.InnerXml;
            id_DataSourceMP.DataBind();
            // Test
            id_writeOut.InnerText = id_DataSourceMP.Data;

            id_GridViewMP.DataSourceID = "id_DataSourceMP";
            id_GridViewMP.DataBind();

            id_DetailsViewMP.DataSourceID = "id_DataSourceMP";
            id_DetailsViewMP.DataBind();
            // Test
            id_DetailsViewMP.PageIndex = 4;
        }

    }

    protected void mp_rowSelected(Object sender, EventArgs e)
    {
        //id_DetailsViewMP.PageIndex = 4;

        id_writeOut.InnerText = id_DataSourceMP.Data;
    }

    protected void id_MapButton_Click(object sender, EventArgs e)
    {
        id_writeOut.InnerText = id_DataSourceMP.Data;
        id_DetailsViewMP.PageIndex = 1;
    }


    //--------------------------------------------------------------------------------------
    //Override Functions
    //--------------------------------------------------------------------------------------

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (GridViewRow row in id_GridViewMP.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {

                row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                row.ToolTip = "Click to select row";
                row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.id_GridViewMP, "Select$" + row.RowIndex, true);
            }
        }

        base.Render(writer);
    }
}

我很确定我有一些与DataBinding相关的思考错误。有人可以告诉我我的失败在哪里或提出替代解决方案吗?

提前感谢您的帮助,请保持温和,这是我的第一篇文章。

0 个答案:

没有答案