繁体   English   中英

如何将XML属性值反序列化为C#对象属性

[英]How to deserialise xml attribute value to C# object attribute

我的Xml如下所示:

  <SearchSuggestion xmlns="http://opensearch.org/searchsuggest2" version="2.0">
  <Query xml:space="preserve">middle ages</Query>
  <Section>

    <Item>
      <Text xml:space="preserve">Middle Ages</Text>
      <Url xml:space="preserve">https://en.wikipedia.org/wiki/Middle_Ages</Url>
      <Description xml:space="preserve">
In the history of Europe, the Middle Ages or medieval period lasted from the 5th to the 15th century. It began with the fall of the Western Roman Empire and merged into the Renaissance and the Age of Discovery.
</Description>
      <Image source="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/JuengeresMathildenkreuz.jpg/35px-JuengeresMathildenkreuz.jpg" width="35" height="50"/>
    </Item>
    <Item>
      <Text xml:space="preserve">Middle Ages in film</Text>
      <Url xml:space="preserve">https://en.wikipedia.org/wiki/Middle_Ages_in_film</Url>
      <Description xml:space="preserve">
Medieval films imagine and portray the Middle Ages through the visual, audio and thematic forms of cinema.
</Description>
      <Image source="https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/Fairbanks_Robin_Hood_standing_by_wall_w_sword.jpg/40px-Fairbanks_Robin_Hood_standing_by_wall_w_sword.jpg" width="40" height="50"/>
    </Item>

  </Section>
</SearchSuggestion>

我的对象如下所示:

[XmlRoot("SearchSuggestion", Namespace = "http://opensearch.org/searchsuggest2")]
public class SearchItem
{

    [XmlElement("Query")]
    public string query { get; set; }
    [XmlElement("Section")]
    public Section section { get; set; }

}
public class Section
{
    [XmlElement("Item")]
    public Item[] items { get; set; }
}
public class Items
{
    [XmlElement("Text")]
    public string Text { get; set; }
    [XmlElement("Url")]
    public string Url { get; set; }
    [XmlElement("Description")]
    public string Description { get; set; }
    [XmlAttribute("source")]
    public string source { get; set; }
}

}

我想存储“源”属性Url,但是运行程序后,我在infoResult数组中始终获取空的源字符串。 我的控制器如下所示:

        string wikiResponse; //raw response from REST endpoint
        SearchItem wikiXmlResponse = null; //Deserialized response


        wikiResponse = await httpClient.GetStringAsync(queryUri);
        var buffer = System.Text.Encoding.UTF8.GetBytes(wikiResponse);
        using (var stream = new MemoryStream(buffer)) {

              var serializer = new XmlSerializer(typeof(SearchItem));
              wikiXmlResponse (SearchItem)serializer.Deserialize(stream);
    }
        Items [] infoResult = wikiXmlResponse.section.items;

您可以尝试以下方法:

public class Items
{
    [XmlElement("Text")]
    public string Text { get; set; }
    [XmlElement("Url")]
    public string Url { get; set; }
    [XmlElement("Description")]
    public string Description { get; set; }
    [XmlElement("Image")]
    public Image Image { get; set; }
}
public class Image
{
    [XmlAttribute("source")]
    public string source { get; set; }
}

您的媒体资源应如下所示,比较XML。 您可以通过以下方式在线将XML代码转换为CSharp类: http://xmltocsharp.azurewebsites.net/

[XmlRoot(ElementName = "Query", Namespace = "http://opensearch.org/searchsuggest2")]
public class Query
{
    [XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string Space { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "Text", Namespace = "http://opensearch.org/searchsuggest2")]
public class Text
{
    [XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string Space { get; set; }
    [XmlText]
    public string text { get; set; }
}

[XmlRoot(ElementName = "Url", Namespace = "http://opensearch.org/searchsuggest2")]
public class Url
{
    [XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string Space { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "Description", Namespace = "http://opensearch.org/searchsuggest2")]
public class Description
{
    [XmlAttribute(AttributeName = "space", Namespace = "http://www.w3.org/XML/1998/namespace")]
    public string Space { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "Image", Namespace = "http://opensearch.org/searchsuggest2")]
public class Image
{
    [XmlAttribute(AttributeName = "source")]
    public string Source { get; set; }
    [XmlAttribute(AttributeName = "width")]
    public string Width { get; set; }
    [XmlAttribute(AttributeName = "height")]
    public string Height { get; set; }
}

[XmlRoot(ElementName = "Item", Namespace = "http://opensearch.org/searchsuggest2")]
public class Items
{
    [XmlElement(ElementName = "Text", Namespace = "http://opensearch.org/searchsuggest2")]
    public Text Text { get; set; }
    [XmlElement(ElementName = "Url", Namespace = "http://opensearch.org/searchsuggest2")]
    public Url Url { get; set; }
    [XmlElement(ElementName = "Description", Namespace = "http://opensearch.org/searchsuggest2")]
    public Description Description { get; set; }
    [XmlElement(ElementName = "Image", Namespace = "http://opensearch.org/searchsuggest2")]
    public Image Image { get; set; }
}

[XmlRoot(ElementName = "Section", Namespace = "http://opensearch.org/searchsuggest2")]
public class Section
{
    [XmlElement(ElementName = "Item", Namespace = "http://opensearch.org/searchsuggest2")]
    public List<Items> Item { get; set; }
}

[XmlRoot(ElementName = "SearchSuggestion", Namespace = "http://opensearch.org/searchsuggest2")]
public class SearchItem
{
    [XmlElement(ElementName = "Query", Namespace = "http://opensearch.org/searchsuggest2")]
    public Query Query { get; set; }
    [XmlElement(ElementName = "Section", Namespace = "http://opensearch.org/searchsuggest2")]
    public Section Section { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
    [XmlAttribute(AttributeName = "version")]
    public string Version { get; set; }
}

暂无
暂无

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

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