积极答复者
string 解析成xml并取得值

问题
-
我访问一个webservice并取得一个string 类型的返回值,返回的是xml如:
<?xml version="1.0" encoding="utf-8" ?>
<sysSetDataFile>
<setBit>1</setBit>
<netConn mode="DHCP" />
<sigOutSet mode="4" value="17" />
<onOffTime group="1" on_time1="00:00:00" off_time1="23:55:00" week1="127" />
<osdLangSet value="0" />
</sysSetDataFile>
现在我要解析这个string 成xml,并取得其中的值,比如 onOffTime xia 的off_time1,怎么取呢?
谢谢各位大侠。- 已移动 Raymond TangModerator 2010年1月15日 7:39 (发件人:ASP.NET 与 AJAX)
答案
-
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Xml; public partial class Default12 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string inputXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<sysSetDataFile>" + "<setBit>1</setBit>" + "<netConn mode=\"DHCP\" />" + "<sigOutSet mode=\"4\" value=\"17\" />" + "<onOffTime group=\"1\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"127\" />" + "<onOffTime group=\"2\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"128\" />" + "<onOffTime group=\"3\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"129\" />" + "<osdLangSet value=\"0\" />" + "</sysSetDataFile>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(inputXML); this.PrintAttribute(doc.ChildNodes); } private void PrintAttribute(XmlNodeList nodes) { foreach (XmlNode node in nodes) { if (node.Name == "onOffTime") { Response.Write(String.Format("group:{0} on_time1:{1} week1:{2}<br />" , node.Attributes["group"].Value , node.Attributes["on_time1"].Value , node.Attributes["week1"].Value)); } this.PrintAttribute(node.ChildNodes); // 递归 } } }
知识改变命运,奋斗成就人生!- 已标记为答案 newshine 2010年1月16日 14:20
全部回复
-
你用这个字符串创建一个XML文件,从XML文件中读取你要的节点值
读取XML参照这里
http://hi.baidu.com/onlyafar/blog/item/8eb1de2a0bfab192023bf610.html
努力+方法=成功 -
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Xml; public partial class Default12 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string inputXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<sysSetDataFile>" + "<setBit>1</setBit>" + "<netConn mode=\"DHCP\" />" + "<sigOutSet mode=\"4\" value=\"17\" />" + "<onOffTime group=\"1\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"127\" />" + "<onOffTime group=\"2\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"128\" />" + "<onOffTime group=\"3\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"129\" />" + "<osdLangSet value=\"0\" />" + "</sysSetDataFile>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(inputXML); this.PrintAttribute(doc.ChildNodes); } private void PrintAttribute(XmlNodeList nodes) { foreach (XmlNode node in nodes) { if (node.Name == "onOffTime") { Response.Write(String.Format("group:{0} on_time1:{1} week1:{2}<br />" , node.Attributes["group"].Value , node.Attributes["on_time1"].Value , node.Attributes["week1"].Value)); } this.PrintAttribute(node.ChildNodes); // 递归 } } }
知识改变命运,奋斗成就人生!- 已标记为答案 newshine 2010年1月16日 14:20
-
private static void TestXml()
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<sysSetDataFile>
<setBit>1</setBit>
<netConn mode=""DHCP"" />
<sigOutSet mode=""4"" value=""17"" />
<onOffTime group=""1"" on_time1=""00:00:00"" off_time1=""23:55:00"" week1=""127"" />
<osdLangSet value=""0"" />
</sysSetDataFile>";
XmlDocument dom = new XmlDocument();
dom.LoadXml(xml);
Console.WriteLine(dom.DocumentElement.SelectSingleNode("onOffTime/@off_time1").Value);
}
楼主可以去了解下Xpath 借助XmlDocument类或者XPathDocument类就可以读取了
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~ -
using System.Xml.Linq;
这是c#3.0
protected void Page_Load(object sender, EventArgs e)
{string xml= "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+ "<sysSetDataFile>"
+ "<setBit>1</setBit>"
+ "<netConn mode=\"DHCP\" />"
+ "<sigOutSet mode=\"4\" value=\"17\" />"
+ "<onOffTime group=\"1\" on_time1=\"00:00:00\" off_time1=\"23:55:00\" week1=\"127\" />"
+ "<osdLangSet value=\"0\" />"
+ "</sysSetDataFile>";
XDocument xd = XDocument.Parse(xml);
string[] atts =( from x in xd.Element("sysSetDataFile").Elements("onOffTime")
select x.Attribute("off_time1").Value).ToArray();
foreach (string att in atts )
Response.Write(att);
}