【发布时间】:2014-05-15 04:53:06
【问题描述】:
所以我目前正在从事一个涉及 C-Sharp 的项目,这让我非常沮丧。目前,我必须获取存储在 XML 标记中的文本,将其作为字符串读取,然后将其存储到一系列文本框中。但是,由于某种原因,文本框不断相互抵消;他们正在互相抹去。我究竟做错了什么?现在已经有一段时间了。谢谢!
XmlDocument generalXML = new XmlDocument();
generalXML.Load(generalURI);
// Connect to the web request for the resource you want
// In other words - create a socket with the uri
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(generalURI);
// Indicate that you will READ only (GET)
req.Method = "GET";
try
{
// get and store the response and convert it into a usable stream
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream str = res.GetResponseStream();
// read the stream as an XML object
XmlReader xr = XmlReader.Create(str);
if (textBoxDescription.Text == "")
{
xr.ReadToFollowing("description");
textBoxDescription.Text = xr.ReadElementContentAsString();
}
if (textBoxWebsite.Text == "")
{
xr.ReadToFollowing("website");
textBoxWebsite.Text = xr.ReadElementContentAsString();
}
if (textBoxEmail.Text == "")
{
xr.ReadToFollowing("email");
textBoxEmail.Text = xr.ReadElementContentAsString();
}
if (textBoxName.Text == "")
{
xr.ReadToFollowing("name");
textBoxName.Text = xr.ReadElementContentAsString();
}
// close the ocnnection to the resource
res.Close();
}
catch
{
Console.Write("Error");
}
【问题讨论】:
-
顺便说一句,您可能不想在 WinForms 中将消息打印到
Console:) 您能给我们generalURI吗? -
generalURI 是这样的:string generalURI = @"simon.ist.rit.edu:8080/Services/resources/ESD" + orgID + "/General";
-
并注明,谢谢。这只是我一直在努力的事情。
-
如果没问题,你也在这里发
orgID?我会在我的机器上尝试您的代码以确定出了什么问题。我现在没有发现任何问题。 -
嗯,orgID是动态生成的……