将 XmlDataSource 动态绑定到上传 XML

Bind XmlDataSource dynamically to uploaded XML

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="id_DataSourceMP"),后者又绑定到 GridView (id="id_GridViewMP") 和 DetailsView (id="id_DetailsViewMP") 控件。 在这个函数中,我可以从 XmlDataSource 读回数据,并且可以更改例如 DetailsView 的 PageIndex。

但在其他函数中 (mp_rowSelected、id_MapButton_Click) 这会产生错误 "Object reference not set to an instance of an object"。

下面是代码:

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 相关的思考错误。有人可以告诉我我的失败在哪里或提出替代解决方案吗?

在此先感谢您的帮助,请保持温柔,这是我的第一个 post。

尤里卡!我找到了解决方案。

我不得不使用 属性 "DataSource" 而不是 "DataSourceIP":

id_DataSourceMP.Data = myDoc.InnerXml;
id_DataSourceMP.DataBind();
id_GridViewMP.DataSource = id_DataSourceMP;
id_GridViewMP.DataBind();

id_DetailsViewMP.DataSource = id_DataSourceMP;
id_DetailsViewMP.DataBind();

随后的错误是 DetailsView 'forgot' 它的数据.. 我以某种方式认为数据绑定意味着控件或数据源永远绑定,就像在婚姻中一样。也许某处有戒指...

但是,每次查看它们时都必须绑定这些东西,即当 DetailsView 应该显示另一个页面时。像这个函数:

private void bindDataXml()
{
    XmlDocument doc_MP = (XmlDocument)Session["Mapping"];

    id_DataSourceMP.Data = doc_MP.InnerXml;
    id_DataSourceMP.DataBind();

    id_Grid_ViewMP.DataSource = id_DataSourceMP;
    id_Grid_ViewMP.DataBind();

    id_DetailsViewMP.DataSource = id_DataSourceMP;
    id_DetailsViewMP.DataBind();
}

也许其他人会觉得这有帮助。