将 Xml 反序列化为 C# 对象时,如何避免因缺少 Xml 元素(例如描述和图像)而导致的异常
How to avoid exception because of some missing Xml Elemets (e.g Description & Image) when deserialising Xml to C# Object
我的 Xml 看起来如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<Item>
<Text xml:space="preserve">Talk:Middle Ages/GA1</Text>
<Url xml:space="preserve">https://en.wikipedia.org/wiki/Talk:Middle_Ages/GA1</Url>
</Item>
<Item>
<Text xml:space="preserve">Talk:Middle Ages/Archive 4</Text>
<Url xml:space="preserve">https://en.wikipedia.org/wiki/Talk:Middle_Ages/Archive_4</Url>
</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 Item
{
[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; }
}
我的控制器如下所示:
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;
for (int i = 0; i < infoResultCount; i++)
{
Title = infoResult[i].Text;
Explaination = infoResult[i].Description;
Image_Url = infoResult[i].Image.source;
Article_Url = infoResult[i].Url;
}
我从维基百科 Api 得到的 Xml 响应,有时响应一些元素,例如缺少描述和图像。避免异常的最佳方法是什么?
没有因缺失元素导致的异常(参见this fiddle)。
这种情况下的异常将是空引用异常,因为您正试图从 null
Image
属性 中读取 source
。你需要处理这个:
Image_Url = infoResult[i].Image?.source;
我的 Xml 看起来如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<Item>
<Text xml:space="preserve">Talk:Middle Ages/GA1</Text>
<Url xml:space="preserve">https://en.wikipedia.org/wiki/Talk:Middle_Ages/GA1</Url>
</Item>
<Item>
<Text xml:space="preserve">Talk:Middle Ages/Archive 4</Text>
<Url xml:space="preserve">https://en.wikipedia.org/wiki/Talk:Middle_Ages/Archive_4</Url>
</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 Item
{
[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; }
}
我的控制器如下所示:
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;
for (int i = 0; i < infoResultCount; i++)
{
Title = infoResult[i].Text;
Explaination = infoResult[i].Description;
Image_Url = infoResult[i].Image.source;
Article_Url = infoResult[i].Url;
}
我从维基百科 Api 得到的 Xml 响应,有时响应一些元素,例如缺少描述和图像。避免异常的最佳方法是什么?
没有因缺失元素导致的异常(参见this fiddle)。
这种情况下的异常将是空引用异常,因为您正试图从 null
Image
属性 中读取 source
。你需要处理这个:
Image_Url = infoResult[i].Image?.source;