读取XML文件---错误 [英] Reading a XML file--- error

查看:92
本文介绍了读取XML文件---错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="retdata.aspx.cs" Inherits="BioData.retdata"%>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
protected void Page_Load(object source, EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/XMLFile/BioData1.xml"));

    XmlNode root = doc.DocumentElement;
    NameLiteral.Text = root.SelectSingleNode("Name").ChildNodes[0].Value;
    AddressLiteral.Text = root.SelectSingleNode("Address").ChildNodes[0].Value;
    DateofBirthLiteral.Text = root.SelectSingleNode("DateofBirth").ChildNodes[0].Value;
    ContactLiteral.Text = root.SelectSingleNode("Contact").ChildNodes[0].Value;
    GenderLiteral.Text = root.SelectSingleNode("Gender").ChildNodes[0].Value;
    OccupationLiteral.Text = root.SelectSingleNode("Occupation").ChildNodes[0].Value;
    MailIDLiteral.Text = root.SelectSingleNode("MailID").ChildNodes[0].Value;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Reading A XML FILE</title>
    <style type="text/css">
        body { font-family: Verdana; font-size: 9pt; }
        .name { background-color: #F7F7F7; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table width="50%" cellpadding="5" cellspacing="2">
        <tr>
            <td class="name">Name</td>
            <td><asp:Literal ID="NameLiteral" runat="server" />
            <asp:Literal ID="AdminLiteral" runat="server" /></td>
        </tr>




<tr>
            <td class="name">Name</td>
            <td><asp:Literal ID="NameLiteral" runat="server" />
            <asp:Literal ID="AdminLiteral" runat="server" /></td>
        </tr>
        <tr>
            <td class="name">Address</td>
            <td><asp:Literal ID="AddressLiteral" runat="server" /></td>
        </tr>
        <tr>
            <td class="name">DateofBirth</td>
            <td><asp:Literal ID="DateofBirthLiteral" runat="server" /></td>
        </tr>
        <tr>
        <td class="name">Contact</td>
        <td><asp:Literal ID="ContactLiteral" runat="server" /></td>
        </tr>
        <tr>
        <td class="name">Gender</td>
        <td><asp:Literal ID="GenderLiteral" runat="server" /></td>
        </tr>
        <tr>
        <td class="name">Occupation</td>
        <td><asp:Literal ID="OccupationLiteral" runat="server" /></td>
        </tr>
        <tr>
        <td class="name">MailID</td>
        <td><asp:Literal ID="MailIDLiteral" runat="server" /></td>
        </tr>
        </table>
    </div>
    </form>
</body>
</html>





--------------------------- -------------------------------------------------- ----------------------------------------



我收到错误---->>>



App_Web_2xe3bb5z.dll中出现System.NullReferenceException类型的异常但未在用户代码中处理



附加信息:对象引用未设置为对象的实例。





i尝试解决所有问题,包括quickwatch但我无法解决这个问题...我实际上试图在asp.net页面中读取XML文件数据...我已经有了xml文件....任何人都可以帮助我....

--------------------------------------------- -------------------------------------------------- -------------------------



XML文件

--------





---------------------------------------------------------------------------------------------------------------------

Im getting an error ---->>>

An exception of type ''System.NullReferenceException'' occurred in App_Web_2xe3bb5z.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.


i tried to solve everything including quickwatch but im not able to solve this... im actually trying to read an XML file data in an asp.net page... i already have the xml file.... Can any one help me out....
------------------------------------------------------------------------------------------------------------------------

XML FILE
--------

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<BioData>
  <BioData isadmin="true">
    <Name>hjmhj</Name>
    <Address>iu</Address>
    <DateofBirth>2-3-1988</DateofBirth>
    <Contact>tty</Contact>
    <Gender>True</Gender>
    <Occupation>ereerhge</Occupation>
    <MailID>abc@abc.com</MailID>
  </BioData>
</BioData>

推荐答案

你没有显示在哪里将抛出异常消息对象引用未设置为对象的实例。



不用担心。这是检测和修复的最简单的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用和它的实例(非静态)成员解除引用,这要求此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它,它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:确保将成员/变量正确初始化为非空引用,或者将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一条记录功能的条件对象引用没有设置为对象的实例 [ ^ ]。



祝你好运,

-SA
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA


请阅读: 3个常见原因的System.NullReferenceException [ ^ ]仔细调试代码。查看异常发生的位置并进行更正。
Please, read this: 3 common causes of System.NullReferenceException[^] carefully and debug your code. See where exception is occurring and correct it.


这篇关于读取XML文件---错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆