我在名为richTextBox
的富文本框中包含此文本:
<notification_counts>
<unseen>0</unseen>
</notification_counts>
<friend_requests_counts>
<unread>1</unread>
<unseen>**0**</unseen>
</friend_requests_counts>
我想从unseen
标记(本例中为0)中提取值,并将其放在名为textbox1
的文本框中。我应该怎么做呢?
完整代码
<?xml version="1.0" encoding="UTF-8"?>
<notifications_get_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
<messages>
<unread>0</unread>
<unseen>0</unseen>
<most_recent>****</most_recent>
</messages>
<pokes>
<unread>0</unread>
<most_recent>0</most_recent>
</pokes>
<shares>
<unread>0</unread>
<most_recent>0</most_recent>
</shares>
<notification_counts>
<unseen>0</unseen>
</notification_counts>
<friend_requests_counts>
<unread>1</unread>
<unseen>0</unseen>
</friend_requests_counts>
<friend_requests list="true">
<uid>***</uid>
</friend_requests>
<group_invites list="true"/>
<event_invites list="true"/>
</notifications_get_response>
答案 0 :(得分:2)
如果您的富文本框仅包含XML标记,您可以解析它以提取您感兴趣的值。例如,使用LINQ to XML:
using System.Xml.Linq;
textBox1.Text = XElement.Parse(richTextBox.Text)
.Descendant("friend_requests_counts")
.Element("unseen").Value;
编辑:由于您的XML标记包含namespaces,因此在选择元素时必须考虑这些因素:
XNamespace fb = "http://api.facebook.com/1.0/";
textBox1.Text = XDocument.Parse(richTextBox.Text).Root
.Element(fb + "friend_requests_counts")
.Element(fb + "unseen").Value;