如何在没有.xml格式的情况下读取XML文件
本文关键字:情况下 读取 XML 文件 格式 xml | 更新日期: 2023-09-27 18:35:39
所以我用netbeans创建了一个简单的RESTful服务,并在localhost中生成了一个XML文件http://localhost:8080/testXML/webresources/entities.categoryid
此页面中只有一个简单的XML文件格式:
<categoryids>
<categoryid>
<CategoryID>id1111</CategoryID>
<CategoryName>Study</CategoryName>
</categoryid>
</categoryids>
我正在使用 C# 读取 xml 文件,将它们放入数据集中,最后放入数据网格视图中。 我该怎么做? 它在本地工作(示例.xml)
ds.ReadXml(Application.StartupPath + "''XML''sample.xml", XmlReadMode.Auto);
dv = ds.Tables[0].DefaultView;
但是它不适用于本地主机
ds.ReadXml("http://localhost:8080/testXML/webresources/entities.categoryid", XmlReadMode.Auto);
有什么办法可以做到这一点吗?
编辑:
NullRferenceException: Object reference not set to an instance of an object.
指示数据网格视图为空
edit2:对不起,它应该是"ReadXml"而不是"WriteXml"
问题是你的 DataSet.ReadXml()-Method 无法从 URL 读取,因为你必须发送一个 GET-Request(参考 http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml%28v=vs.80%29.aspx)。您需要发送一个 Web 请求才能与您的 RESTful-Service 对话:
WebRequest request = WebRequest.Create ("http://localhost:8080/testXML/webresources/entities.categoryid");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
Stream dataStream = response.GetResponseStream ();
ds.ReadXml(dataStream);
dataStream.Close();
response.Close();
...
我希望这对你有用。