我想在Xml中读取xml数据,但问题是url有请求参数,这使我无法读取xml数据w。我所知道的是像xamarin这样的标准读取数据xml
var document = XDocument.Load("http://www.forex.se/ratesxml.asp? id=492");
var row = document.Descendants("row").FirstOrDefault();
if(row != null)
{
var sellCash = row.Element("sell_cash")?.Value;
USDSellLabel.Text = sellCash;
}
我的Request Xml是
<?xml version='1.0' encoding='UTF-8'?>
<Request param="name" param="name" param="name">
</Request>
所以我的问题是如何将该请求输入到我的代码中以便我可以获取/读取xml数据?
这是我在Postman中尝试从URL返回的xml
<?xml version="1.0" encoding="UTF-8"?>
<Response type="service-response">
<ResponseCode>00</ResponseCode>
<ResponseStatus>success</ResponseStatus>
<ResponseMessage>
<Accounts>
<Account Name= 'Payments' AvBal ='0 USD' ResBal ='0.00 S$' Currency ='USD' LocalBalance ='0.00 MYR' defaultW ='false' />
</Accounts>
<Accounts>
<Account Name= 'Point' AvBal ='100 LP' ResBal ='0.00' Currency ='LP' LocalBalance ='100.00 MYR' defaultW ='false' />
</Accounts>
<Accounts>
<Account Name= 'AccountMoney' AvBal ='381000 USD' ResBal ='0.00 S$' Currency ='USD' LocalBalance ='116.08 MYR' defaultW ='true' />
</Accounts>
<PdfUrl>http://123.123.123.123:7171/file?a123123.pdf</PdfUrl>
</ResponseMessage>
</Response>
答案 0 :(得分:0)
使用xml linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication14
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
var response = doc.Elements("Response").Select(x => new
{
type = (string)x.Attribute("type"),
code = (int)x.Element("ResponseCode"),
status = (string)x.Element("ResponseStatus"),
message = x.Descendants("Account").Select(y => new
{
name = (string)y.Attribute("Name"),
avBal = (string)y.Attribute("AvBal"),
resBal = (string)y.Attribute("ResBal"),
currency = (string)y.Attribute("Currency"),
localBalance = (string)y.Attribute("LocalBalance"),
defaultw = (Boolean)y.Attribute("defaultW")
}).ToList()
}).FirstOrDefault();
}
}
}