Skip to main content
 首页 » 编程设计

c#之从 xml REST POST webResponse 解析 C# 中的 XML

2024年01月31日28telwanggs

我正在尝试解析来自 REST 端点的 XML 响应。我正在使用 POST 请求(如果相关的话)。我无法正确解析文档。我已经研究了整个堆栈溢出,但似乎没有一个适用于我的回复。 我如何解析我的 XML?我已经尝试了以下所有方法:

使用 XDocument:

Stream postData = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(postData, Encoding.UTF8); 
string result = reader.ReadToEnd(); 
var document = XDocument.Parse(result); 
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom"); 
var items = document.Descendants("NameFirst") 
                    .ToDictionary(i => (string)i.Attribute("Key"), 
                                  i => (string)i.Attribute("Value")); 

对每个 XElements 使用嵌套:

Stream postData = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(postData, Encoding.UTF8); 
string result = reader.ReadToEnd(); 
XDocument document = XDocument.Parse(result); 
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom"); 
string list = ""; 
foreach (XElement level1 in document.Elements("feed")) 
{ 
    foreach (XElement level2 in level1.Elements("entry")) 
    { 
        foreach (XElement level3 in level2.Elements("content")) 
        { 
            foreach (XElement entry in level3.Elements("Entry")) 
            { 
                list += entry.Attribute("NameFirst").Value; 
                list += entry.Attribute("NameLast").Value; 
            } 
        } 
    } 
} 

使用 Descendant 方法的串联:

Stream postData = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(postData, Encoding.UTF8); 
string result = reader.ReadToEnd(); 
var document = XDocument.Parse(result); 
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom"); 
var listOfFields = document.Descendants(ns + "feed").Descendants(ns + "entry").Descendants(ns + "content").Descendants(ns + "entry").Select(x => x.Elements().First().Value).ToList(); 
ResultLabel.Text = ""; 
foreach(String item in listOfFields){ 
    ResultLabel.Text += item; 
} 

使用多个元素方法:

Stream postData = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(postData, Encoding.UTF8); 
string result = reader.ReadToEnd(); 
var document = XDocument.Parse(result); 
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom"); 
var listOfFields = document.Descendants(ns + "feed").Descendants(ns + "entry").Descendants(ns + "content").Descendants(ns + "entry").Select(x => x.Elements().First().Value).ToList(); 
var entry_ = document.Root.Element("entry"); 
var content = entry_.Element("content"); 
var entry = content.Element("Entry"); 
var fname = entry.Element("NameFirst").Value; 
var lname = entry.Element("NameLast").Value; 
ResultLabel.Text = string.Join(",", lname, fname); 

我的 XML 响应:

<?xml version="1.0" encoding="utf-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <title type="text">Search Results</title> 
    <id>https://myendpoint.com/Rest/services/select/Entry</id> 
    <updated>2015-10-07T09:51:32Z</updated> 
    <link rel="self" type="application/atom+xml" href="https://myendpoint.com/Rest/services/select/Entry" /> 
    <entry> 
        <id>https://myendpoint.com/services/select/Entry/26032</id> 
        <title type="text">Smith, John</title> 
        <published>2013-10-08T10:14:20Z</published> 
        <updated>2015-10-02T12:14:18Z</updated> 
        <contributor><name>WebUser</name></contributor> 
            <content type="xhtml"> 
            <Entry> 
                <EntryID>26032</EntryID> 
                <CategoryID>0</CategoryID> 
                <EventID>0</EventID> 
                <GroupID>0</GroupID> 
                <ContactID>0</ContactID> 
                <AddressTypeID>0</AddressTypeID> 
                <PinNumber>0000</PinNumber> 
                <Password></Password> 
                <PortalEmail></PortalEmail> 
                <NameLast>Smith</NameLast> 
                <NameFirst>John</NameFirst> 
                <NameTitle></NameTitle> 
                <NamePreferred>Mike</NamePreferred> 
                <NameWeb>smith</NameWeb> 
                <NameOther></NameOther> 
                <NameInitials></NameInitials> 
                <NameSharer></NameSharer> 
                <GenderEnum>Female</GenderEnum> 
                <Birth_GenderEnum>Male</Birth_GenderEnum> 
                <DirectoryFlagPrivacy>false</DirectoryFlagPrivacy> 
                <Position></Position> 
                <ID1>123456</ID1> 
                <ID2>smith</ID2> 
                <ID3></ID3> 
                <ID4>0</ID4> 
                <ID5>0</ID5> 
                <PhoneProcessToAccount>true</PhoneProcessToAccount> 
                <PhoneChargeTypeID>0</PhoneChargeTypeID> 
                <PhoneDisableValue>0</PhoneDisableValue> 
                <PhoneRestrictValue>0</PhoneRestrictValue> 
                <PhoneControlEnum>Default</PhoneControlEnum> 
                <TaxExemptionEnum>None</TaxExemptionEnum> 
                <Testing>true</Testing> 
                <timestamp>000007975236</timestamp> 
            </Entry> 
        </content> 
    </entry> 
</feed> 

请您参考如下方法:

您可以将 XML 反序列化为易于访问的对象。方法如下:

创建将保存反序列化数据的对象:

您可以通过复制从 REST 端点获取的 XML 轻松完成此操作,然后在 Visual Studio 中转到 Edit -> Paste Special -> 将 XML 粘贴为类

创建类后,您将得到如下内容:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2005/Atom", IsNullable = false)] 
public partial class feed 
{ 
 
    public feedTitle title { get; set; } 
 
    public string id { get; set; } 
 
    public System.DateTime updated { get; set; } 
 
    public feedLink link { get; set; } 
 
    public feedEntry entry { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedTitle 
{ 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string type { get; set; } 
 
    [System.Xml.Serialization.XmlTextAttribute()] 
    public string Value { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedLink 
{ 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string rel { get; set; } 
 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string type { get; set; } 
 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string href { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedEntry 
{ 
 
    public string id { get; set; } 
 
    public feedEntryTitle title { get; set; } 
 
    public System.DateTime published { get; set; } 
 
    public System.DateTime updated { get; set; } 
 
    public feedEntryContributor contributor { get; set; } 
 
    public feedEntryContent content { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedEntryTitle 
{ 
 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string type { get; set; } 
 
    [System.Xml.Serialization.XmlTextAttribute()] 
    public string Value { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedEntryContributor 
{ 
 
    public string name { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedEntryContent 
{ 
 
    public feedEntryContentEntry Entry { get; set; } 
 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string type { get; set; } 
} 
 
 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2005/Atom")] 
public partial class feedEntryContentEntry 
{ 
 
    public ushort EntryID { get; set; } 
 
    public byte CategoryID { get; set; } 
 
    public byte EventID { get; set; } 
 
    public byte GroupID { get; set; } 
 
    public byte ContactID { get; set; } 
 
    public byte AddressTypeID { get; set; } 
 
    public byte PinNumber { get; set; } 
 
    public object Password { get; set; } 
 
    public object PortalEmail { get; set; } 
 
    public string NameLast { get; set; } 
 
    public string NameFirst { get; set; } 
 
    public object NameTitle { get; set; } 
 
    public string NamePreferred { get; set; } 
 
    public string NameWeb { get; set; } 
 
    public object NameOther { get; set; } 
 
    public object NameInitials { get; set; } 
 
    public object NameSharer { get; set; } 
 
    public string GenderEnum { get; set; } 
 
    public string Birth_GenderEnum { get; set; } 
 
    public bool DirectoryFlagPrivacy { get; set; } 
 
    public object Position { get; set; } 
 
    public uint ID1 { get; set; } 
 
    public string ID2 { get; set; } 
 
    public object ID3 { get; set; } 
 
    public byte ID4 { get; set; } 
 
    public byte ID5 { get; set; } 
 
    public bool PhoneProcessToAccount { get; set; } 
 
    public byte PhoneChargeTypeID { get; set; } 
 
    public byte PhoneDisableValue { get; set; } 
 
    public byte PhoneRestrictValue { get; set; } 
 
    public string PhoneControlEnum { get; set; } 
 
    public string TaxExemptionEnum { get; set; } 
 
    public bool Testing { get; set; } 
 
    public uint timestamp { get; set; } 
} 

然后您可以将 XML 反序列化为 feed 对象,如下所示:

using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("Your XML string here"))) 
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(feed)); 
    feed feedObject = (feed)serializer.Deserialize(ms); 
 
    // Then you can access the xml content like you do with any object. 
    Console.WriteLine(feedObject.title.Value); 
} 

Live demo