【问题标题】:How to add on text from a certain XML file onto text boxes如何将某个 XML 文件中的文本添加到文本框中
【发布时间】: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是动态生成的……

标签: c# xml


【解决方案1】:

好的,这是从您的问题中得到的:

首先你的 URI 写的不正确。

@"simon.ist.rit.edu:8080/Services/resources/ESD/"; + orgID + "/General";

将其更改为: (@"simon.ist.rit.edu:8080/Services/resources/ESD/" + orgID + "/General"

其次,我认为文本框没有正确填充,它们不会相互抵消。 试试:

XmlDocument document = new XmlDocument();
document.Load(@"http://simon.ist.rit.edu:8080/Services/resources/ESD/Organizations");
XmlNode OrganizationID= document.DocumentElement.SelectSingleNode("/data/row/OrganizationID");
string type= document.DocumentElement.SelectSingleNode("/data/row/type").InnerText.ToString();
string Name= document.DocumentElement.SelectSingleNode("/data/row/Name").InnerText.ToString();
string Email= document.DocumentElement.SelectSingleNode("/data/row/Email").InnerText.ToString();
string city= document.DocumentElement.SelectSingleNode("/data/row/city").InnerText.ToString();
string zip= document.DocumentElement.SelectSingleNode("/data/row/zip").InnerText.ToString();
string CountyName= document.DocumentElement.SelectSingleNode("/data/row/CountyName").InnerText.ToString();
string State= document.DocumentElement.SelectSingleNode("/data/row/State").InnerText.ToString();

【讨论】:

  • 等等,URI 中的分号不应该存在...它不在我的初始代码中。不,那没有用...
  • @DanielAshfallZhou 无论如何,请在您的代码中使用该 Http 碎屑,只需 XmlDocument 并将其用作 DOT_NET Junior 向您展示的内容,您应该没问题。
  • 不,但是那个分号不在我的初始代码中;我不知道它是怎么落到这里的。它没有用......
  • @DanielAshfallZhou 在 URL 上给出了您的 xml:simon.ist.rit.edu:8080/Services/resources/ESD/Organizations 我看不到有描述节点。
  • 描述是您通过访问特定组织获得的 xml 的一部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多