如何将特定XML文件中的文本添加到文本框中

时间:2014-05-15 04:53:06

标签: c# xml

所以我现在正在开展一个涉及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");
        }

1 个答案:

答案 0 :(得分: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();