如何使用C#访问特定的XML元素
发布时间:2020-12-16 01:54:34 所属栏目:百科 来源:网络整理
导读:我正在使用只有 XML的API,因为我已经使用 XML做了很多事情. 回复如下. 如何获得 status的值.我试过这样做: XmlNodeList results = xmlDoc.GetElementsByTagName("ProcessRequestResult"); 但我最终得到了充满XML的InnerText,我无法弄清楚如何正确解析. ?xml
我正在使用只有
XML的API,因为我已经使用
XML做了很多事情.
回复如下. 如何获得< status>的值.我试过这样做: XmlNodeList results = xmlDoc.GetElementsByTagName("ProcessRequestResult"); 但我最终得到了充满XML的InnerText,我无法弄清楚如何正确解析. <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <ProcessRequestResponse> <ProcessRequestResult> <ConsumerAddEntryResponse> <Status>Failed</Status> 解决方法
如何使用Linq To Xml?
var xDoc = XDocument.Parse(xml); //OR XDocument.Load(filename) string status = xDoc.Descendants("Status").First().Value; 编辑 我用过的xml测试 string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <ProcessRequestResponse> <ProcessRequestResult> <ConsumerAddEntryResponse> <Status>Failed</Status> </ConsumerAddEntryResponse> </ProcessRequestResult> </ProcessRequestResponse> </soap:Body> </soap:Envelope>"; 编辑2 string xml = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <ProcessRequestResponse xmlns=""http://submission.api.domain/""> <ProcessRequestResult> <ConsumerAddEntryResponse> <Status>Failed</Status> </ConsumerAddEntryResponse> </ProcessRequestResult> </ProcessRequestResponse> </soap:Body> </soap:Envelope>"; var xDoc = XDocument.Parse(xml); XNamespace ns = "http://submission.api.domain/"; string status = xDoc.Descendants(ns + "Status").First().Value; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |