使用xml字符串[已解决] [英] Working with a xml string [Solved]
问题描述
我使用以下代码检索网页的源xml:
I retrieve the source xml of a web page using the following code:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
using (Stream stream = request.GetResponse().GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string response = reader.ReadToEnd();
}
}
通常在处理xml文件时,我会使用以下方法来读取它:
Usually when working with xml files I would use the following method to read it:
XmlDocument xmlDoc = new XmlDocument();
XmlTextReader xmlReader = new XmlTextReader(string.Format(@"{0}\config.xml", pathToConfig));
xmlDoc.Load(xmlReader);
int NodeNum = 0;
if (xmlDoc.ChildNodes[1].ChildNodes.Count > 0)
{
XmlNode node = null;
for (int i = 0; i < xmlDoc.ChildNodes[1].ChildNodes.Count; i++)
{
node = xmlDoc.ChildNodes[1].ChildNodes[i];
if (node.Name.ToString() == "serverIP")
{
checkServer = true;
NodeNum = i;
break;
}
}
if (checkServer)
{
XmlNodeList nodeList = xmlDoc.ChildNodes[1].ChildNodes[NodeNum].ChildNodes;
string serverID = nodeList[0].InnerText.ToString();
}
}
我的问题是,我该如何修改以上代码以与从Web返回的字符串一起使用,该字符串基本上是xml的一个长字符串,而不是文件.任何帮助将不胜感激.
没关系,我想出了解决方法,我只使用XmlDocument的LoadXML而不是Load.
[edit]在主题中添加了已解决"-OriginalGriff [/edit]
My question is this, how can I modify the above code to work with the returned string from the web, which is basically one long string of xml, rather than with a file. Any help would be appreciated.
It''s okay I figured out how to do it I just use LoadXML of the XmlDocument rather than Load.
[edit]Added "Solved" to subject - OriginalGriff[/edit]
推荐答案
已解决,只能从未答复的列表中删除-由OP解决
Answered only to remove from the unanswered list - solved by OP
无论您做什么,解析XML都不是一个好习惯.
我建议您 HTML敏捷包 [ Nish [
Whatever you''re doing for parsing XML isn''t a good practice at all.
I would suggest you to HTML Agility pack[^], which is helpful for parsing HTML.
or alternatively see Article by Nish[^] a very well known author in Code project. See how he has parsed HTML in his code.
这篇关于使用xml字符串[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!