简体   繁体   English

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

[英]Parsing XML in C# from xml REST POST webResponse

I'm trying to parse an XML response from a REST endpoint. 我正在尝试从REST端点解析XML响应。 I am using a POST request (if that's relevant). 我正在使用POST请求(如果相关)。 I can't get the document to parse correctly. 我无法正确解析文档。 I've researched this all over stack overflow, and none seem to be applicable to my response. 我已经对整个堆栈溢出进行了研究,但似乎没有一个适用于我的响应。 How can i parse my XML? 如何解析我的XML? I've tried all of the below: 我已经尝试了以下所有方法:

Using XDocument: 使用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"));

Using nested for each with XElements: 对XElement使用每个嵌套:

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;
            }
        }
    }
}

Using a concatenation of Descendant methods: 使用后代方法的串联:

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;
}

Using multiple Element methods: 使用多种Element方法:

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);

My XML response: 我的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>

You can deserialize the XML into easily accessible objects. 您可以将XML反序列化为易于访问的对象。 Here's how to do it: 方法如下:

Create the objects that will hold your deserialized data: 创建将保存反序列化数据的对象:

You can easily do it by copying the XML you're getting from your REST endpoint then in Visual Studio go to Edit -> Paste Special -> Paste XML as classes 您可以通过复制从REST端点获取的XML轻松完成此操作,然后在Visual Studio中转到“ Edit ->“ Paste Special ->“将Paste XML as classes

在此处输入图片说明

Once your classes are created you will get something like this: 创建课程后,您将获得以下内容:

[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; }
}

Then you can deserialize the XML into a feed object like the following: 然后,您可以将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 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM