我在 C# 中有一個執行 httpclient 獲取請求的 Windows 表單。這是 XML 格式的回應
> <Result><Success>true</Success><Token>MYTOKENHERE</Token><TokenExpirationDate null="1"
> /><UserName>********</UserName><PersonCode>442078</PersonCode><LoginStatusMessage>LoginOk</LoginStatusMessage></Result>
我想將文本框的文本設定為<Token></Token>標簽之間的內容
做到這一點的最佳方法是什么
謝謝
這是我當前的 Form1.cs 代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Xml.Serialization;
namespace EBS_Token_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
HttpClient Client = new HttpClient();
Client.BaseAddress = new Uri("PRIVATE_URL");
string Username = username.Text;
string Password = password.Text;
string CredentialsString = $"{Username}:{Password}";
byte[] CredentialsStringByes = System.Text.Encoding.UTF8.GetBytes(CredentialsString);
Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("basic", Convert.ToBase64String(CredentialsStringByes));
try
{
var Response = Client.GetAsync("Rest/Authentication").Result;
if (!Response.IsSuccessStatusCode)
{
// Something went wrong, is error.
// Put a breakpoint on the line below and we can figure out why.
string x = "";
}
string ServerResponse = Response.Content.ReadAsStringAsync().Result;
}
catch (Exception ex)
{
return;
}
}
如果我將文本框的值設定為ServerResponse它具有完整的 xml 檔案。我只需要提取 .
uj5u.com熱心網友回復:
通常我會為這樣的事情使用XmlSerializer,但如果你真的只需要這個值,你可能想嘗試XElement:
var root = XElement.Parse(yourResponseString);
var value = root.Element("Token")?.Value;
XElement 非常適合遍歷、讀取和操作 XML。
uj5u.com熱心網友回復:
XPath 適用于此,盡管有些人可能認為矯枉過正。XPath 運算式 /Result/Token/Text() 完全符合它的樣子。
using System.Xml;
using System.Xml.XPath;
string Incoming_XML = @"<Result><Success>true</Success><Token>MYTOKENHERE</Token><TokenExpirationDate null=""1"" /><UserName>********</UserName><PersonCode>442078</PersonCode><LoginStatusMessage>LoginOk</LoginStatusMessage></Result>";
XPathDocument xPathDoc = null;
using (StringReader sr = new StringReader(Incoming_XML))
{
xPathDoc = new XPathDocument(sr);
XPathNavigator xPathNav = xPathDoc.CreateNavigator();
string Result = xPathNav.SelectSingleNode("/Result/Token/text()").Value;
Console.WriteLine($"Token is: {Result}");
}
uj5u.com熱心網友回復:
下面的示例代碼可能不是最好的,但它很容易理解并且可以完成作業。
// Define a regular expression pattern
string Regex_syntax = @"<Token>[\s\S]*?</Token>";
Regex rx = new Regex(Regex_syntax,RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a RAW data string to process , in your case will be http response result
string inputtext = @"<Result><Success>true</Success><Token>MYTOKENHERE</Token><TokenExpirationDate";
// Find matches.
MatchCollection matches = rx.Matches(inputtext);
// clease the uneeded string
string cleanseString = matches[0].ToString().Replace(@"<Token>", "");
cleanseString = cleanseString.Replace(@"</Token>", "");
// This will print value in between the token tag
Console.WriteLine("require output = " cleanseString);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493447.html
上一篇:找出資料框中的值是否增加了十位
下一篇:捕獲XML中的注釋文本
