如何获取XML文档并将信息显示在文本框中? [英] How do I take a XML Document and display information into text boxes?

查看:96
本文介绍了如何获取XML文档并将信息显示在文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取看起来像这样的XML文档.
我的C#代码.

I am trying to take an XML document that look it this.
My C# code.

public void loadXML()
{
	ProjectReferralTableControlRow rows = new ProjectReferralTableControlRow();



	System.Xml.XmlReader obj = System.Xml.XmlReader.Create(@"c:\temp\reg.xml");
	{
		obj.Read();
		obj.ReadStartElement("Registration");
		obj.ReadStartElement("BusinessName",  rows.SubConstructionManager1.Text);
		obj.ReadStartElement("OwnerName", rows.SubContactName1.Text);
		obj.ReadStartElement("Address", rows.SubAddress1.Text);
		obj.ReadStartElement("City", rows.SubCity1.Text);
		obj.ReadStartElement("State", rows.SubState1.Text);
		obj.ReadStartElement("Zipcode", rows.SubZip1.Text);
		obj.ReadStartElement("Phone", rows.SubPhone1.Text);
		obj.ReadStartElement("Fax", rows.SubFax1.Text);
		obj.ReadStartElement("EmailAddress", rows.SubEmail1.Text);

	}
	obj.ReadEndElement();								
}



出现以下错误.



getting the following error.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 


Line 979:                obj.Read();
Line 980:                obj.ReadStartElement("Registration");
<big></big>Line 981:                obj.ReadStartElement("BusinessName",  rows.SubConstructionManager1.Text);
Line 982:                obj.ReadStartElement("OwnerName", rows.SubContactName1.Text);
Line 983:                obj.ReadStartElement("Address", rows.SubAddress1.Text);

推荐答案

整个方法都是不好的,因为您对XML内容进行了假设.您还可以在代码中使用硬编码的立即字符串常量,这总是很糟糕,应该改为使其成为数据驱动的,例如,可以将XSD用作有效元素的来源.

因此,与其尝试阅读您所期望的内容,不如尝试全部阅读,直到阅读完文档为止,并尝试与您在旅途中看到的内容相匹配.从此页面上的示例开始: http://msdn.microsoft.com/zh-cn/library/system.xml.xmlreader.read.aspx [
The whole approach is bad, because you make assumptions on XML contents. You also use hard-coded immediate string constants in you code, which is always bad, you should make it data-driven instead, for example you can use XSD as a source of valid elements.

So, instead of trying to read what you''re expected, try to read all until you read the document to its end and try to match what you can see on the road. Start from the sample on this page: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read.aspx[^].

—SA


您可以创建 XML模式 [ ^ ]用于您的xml文档-此还将有助于提高xml文件的完整性.

一旦有了架构,就可以使用 xsd [ ^ ]为您创建一个数据集xml文件.

可以使用 DataSet.ReadXml [ ^ ].

现在,您已经有了一个包含xml文件数据的数据集,您可以使用Visual Studio的设计功能在数据集和UI元素之间设置绑定.

问候
Espen Harlinn
You could create an XML Schema[^] for your xml document - this wil also help to improve the integrity of your xml file.

Once you have a schema, you can use xsd[^] to create a dataset for your xml file.

Content can be loaded into the dataset using the DataSet.ReadXml[^].

Now that you have a dataset containing the data for your xml file, you can set up bindings between your dataset and the UI elements using the design features of Visual Studio.

Regards
Espen Harlinn


您是否尝试调试并查看这些行周围的值是什么?

看起来这部分有问题:rows.SubConstructionManager1.Text
检查rows rows.SubConstructionManager1 是否为不为空
Did you try to DEBUG and see what are the values around the lines?

Looks like this part has issues: rows.SubConstructionManager1.Text
Check if rows or rows.SubConstructionManager1 is not null


这篇关于如何获取XML文档并将信息显示在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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