最近在介紹第三方金流服務時,廠商使用 XML 回傳,久沒解析 XML,剛收到時還愣了一下,突然間想不起來該怎麼做,看來有年紀後就不適合再靠記憶力,馬上來紀錄一下。
"\t\t\t\t\t\t\t\t\t\t\t\t<?xml version=\"1.0\" encoding=\"UTF-8\"?><documents><Resp><Id>486441f5-7fa6-4676-b37d-ef29cfbdb1fe</Id><accountId>yowko</accountId><orderNo>Yowko16111800018</orderNo><mtaTransId>8800000006293669</mtaTransId><transAmt>3</transAmt><result>0000</result><respCode>0</respCode><transTime>20161118143324</transTime><completeTime>20161118150716</completeTime></Resp></documents>";
\t
讓我覺得應該經過 html encode,所以先 html decode
HttpUtility.HtmlDecode(xml);
trim
HttpUtility.HtmlDecode(xml).Trim();
Edit --> Paste Special --> Paste XML as Classes
Namespace:System.Xml.Serialization
var reader = new StringReader(xmlstring);//xmlstring 是傳入 XML 格式的 string
var serializer = new XmlSerializer(typeof(documents));//documents 是 paste xml as class 來的類別
var instance = (documents)serializer.Deserialize(reader);
string xml = "\t\t\t\t\t\t\t\t\t\t\t\t<?xml version=\"1.0\" encoding=\"UTF-8\"?><documents><Resp><Id>486441f5-7fa6-4676-b37d-ef29cfbdb1fe</Id><accountId>yowko</accountId><orderNo>Yowko16111800018</orderNo><mtaTransId>8800000006293669</mtaTransId><transAmt>3</transAmt><result>0000</result><respCode>0</respCode><transTime>20161118143324</transTime><completeTime>20161118150716</completeTime></Resp></documents>";
var reader = new StringReader(HttpUtility.HtmlDecode(xml).Trim());
var serializer = new XmlSerializer(typeof(documents));
var instance = (documents)serializer.Deserialize(reader);
XmlDocument
Namaspace:System.Xml
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);//xmlstring 是傳入 XML 格式的 string
doc.GetElementsByTagName("result")[0]?.InnerText;//取得第一個 result 節點內的值
string xml = "\t\t\t\t\t\t\t\t\t\t\t\t<?xml version=\"1.0\" encoding=\"UTF-8\"?><documents><Resp><Id>486441f5-7fa6-4676-b37d-ef29cfbdb1fe</Id><accountId>yowko</accountId><orderNo>Yowko16111800018</orderNo><mtaTransId>8800000006293669</mtaTransId><transAmt>3</transAmt><result>0000</result><respCode>0</respCode><transTime>20161118143324</transTime><completeTime>20161118150716</completeTime></Resp></documents>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(HttpUtility.HtmlDecode(xml).Trim());
doc.GetElementsByTagName("result")[0]?.InnerText;
XDocument
Namespace:System.Xml.Linq
XDocument doc = XDocument.Parse(xmlstring);//xmlstring 是傳入 XML 格式的 string
doc.Descendants().FirstOrDefault(e => e.Name.ToString().ToLower().Contains("result"))?.Value;//取得第一個 result 節點內的值
string xml = "\t\t\t\t\t\t\t\t\t\t\t\t<?xml version=\"1.0\" encoding=\"UTF-8\"?><documents><Resp><Id>486441f5-7fa6-4676-b37d-ef29cfbdb1fe</Id><accountId>yowko</accountId><orderNo>Yowko16111800018</orderNo><mtaTransId>8800000006293669</mtaTransId><transAmt>3</transAmt><result>0000</result><respCode>0</respCode><transTime>20161118143324</transTime><completeTime>20161118150716</completeTime></Resp></documents>";
XDocument doc = XDocument.Parse(HttpUtility.HtmlDecode(xml).Trim());
DOC.Descendants().FirstOrDefault(e => e.Name.ToString().ToLower().Contains("result"))?.Value;