【问题标题】:While binding Treeview control to xmldatasource, "Exception:Root element is missing " is occured将 Treeview 控件绑定到 xmldatasource 时,出现“异常:缺少根元素”
【发布时间】:2012-11-21 09:39:08
【问题描述】:

aspx 文件:

  <div>
    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" 
        ShowCheckBoxes="All" ShowLines="True">
    </asp:TreeView>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Details.xml">
    </asp:XmlDataSource>
  </div>

详细信息.xml:

 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <Root>
 <Collection Name="Server 1">
  <Box>MB1</Box>
  <Box>MB2</Box>
  <Box>MB3</Box>
  <Box>MB4</Box>
  <Box>MB5</Box>
  </Collection>
 </Root>

堆栈跟踪:

 [XmlException: Root element is missing.]
 System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
 System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) +61
 System.Xml.XmlTextReaderImpl.ParseDocumentContent() +3981048
 System.Xml.XmlTextReaderImpl.Read() +151
 System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +58
 System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +129
 System.Xml.XmlDocument.Load(XmlReader reader) +108
 System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument document, CacheDependency& dataCacheDependency, CacheDependency& transformCacheDependency) +306
 System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +153
 System.Web.UI.WebControls.XmlHierarchicalDataSourceView.Select() +17
 System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +125
 System.Web.UI.WebControls.TreeView.PerformDataBinding() +120
 System.Web.UI.WebControls.HierarchicalDataBoundControl.PerformSelect() +85
 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
 System.Web.UI.WebControls.TreeView.DataBind() +4
 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
 System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
 System.Web.UI.WebControls.TreeView.OnPreRender(EventArgs e) +36
 System.Web.UI.Control.PreRenderRecursiveInternal() +80
 System.Web.UI.Control.PreRenderRecursiveInternal() +171
 System.Web.UI.Control.PreRenderRecursiveInternal() +171
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

加载页面时抛出异常,我尝试根据 w3cshools 验证器验证 xml 文件及其有效。什么事情会导致这个异常。纠正我哪里出错了。谢谢!

【问题讨论】:

    标签: asp.net xml treeview xmldatasource


    【解决方案1】:

    尝试如下更改一次树视图代码

    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
    <DataBindings>  
     <asp:TreeNodeBinding DataMember="Collection" TextField="Name" />  
      <asp:TreeNodeBinding DataMember="Box" TextField="Name" />  
    
     </DataBindings> 
    </asp:TreeView>
    

    XML

    <?xml version="1.0" encoding="utf-8" ?>  
     <Collection Name="Server 1">
      <Box Name="MB1"></Box>
      <Box Name="MB2"></Box>
      <Box Name="MB3"></Box>
      <Box Name="MB4"></Box>
     </Collection>
    

    TreeView Example

    【讨论】:

    【解决方案2】:
    <div>
       <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Details.xml">
       </asp:XmlDataSource>
       <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" 
        ShowCheckBoxes="All" ShowLines="True">
          <DataBindings>  
              <asp:TreeNodeBinding DataMember="Collection" TextField="Name" />  
              <asp:TreeNodeBinding DataMember="Box" TextField="Name" />  
         </DataBindings> 
      </asp:TreeView>
    </div>
    

    我刚刚更改了 xmldatasource 的位置,它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 2012-04-24
      • 2013-05-31
      • 2013-03-23
      • 2023-03-15
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多