richTextbox1 to textbox1?

本文关键字:textbox1 to richTextbox1 | 更新日期: 2023-09-27 18:06:54

我有一个富文本框命名为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>

richTextbox1 to textbox1?

如果您的富文本框只包含XML标记,您可以解析它以提取您感兴趣的值。例如,使用LINQ to XML:

using System.Xml.Linq;
textBox1.Text = XElement.Parse(richTextBox.Text)
                        .Descendant("friend_requests_counts")
                        .Element("unseen").Value;

EDIT:由于XML标记包含名称空间,因此在选择元素时必须考虑它们:

XNamespace fb = "http://api.facebook.com/1.0/";
textBox1.Text = XDocument.Parse(richTextBox.Text).Root
                         .Element(fb + "friend_requests_counts")
                         .Element(fb + "unseen").Value;