C# 为什么这个代码不起作用?XML with.StartsWith
为什么这样做:C# 为什么这个代码不起作用?XML with.StartsWith,c#,xml,windows-phone-7,linq-to-xml,C#,Xml,Windows Phone 7,Linq To Xml,为什么这样做: XDocument dataFeed = XDocument.Parse(e.Result); var guide = from query in dataFeed.Descendants("MaxPayne3") select new NewGamesClass {
XDocument dataFeed = XDocument.Parse(e.Result);
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameID = (string)query.Element("ID"),
GameTitle = (string)query.Element("Title"),
GameDescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
if (selectedIndex == "0")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Feel"));
else if (selectedIndex == "1")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));
if (selectedIndex == "0")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("000"));
else if (selectedIndex == "1")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("001"));
但不是这样:
XDocument dataFeed = XDocument.Parse(e.Result);
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameID = (string)query.Element("ID"),
GameTitle = (string)query.Element("Title"),
GameDescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
if (selectedIndex == "0")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Feel"));
else if (selectedIndex == "1")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));
if (selectedIndex == "0")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("000"));
else if (selectedIndex == "1")
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("001"));
我想用GameID代替GameTitle
对不起,没有XML,对我来说太早了,哈哈
在这里:
下面是课堂:
public class NewGamesClass
{
string gameID;
string gameTitle;
string gamedescription;
string gameImage;
string gameGuide;
string videoLink;
public string GameID
{ get { return gameID; } set { gameID = value; } }
public string GameTitle
{ get { return gameTitle; } set { gameTitle = value; } }
public string GameDescription
{ get { return gamedescription; } set { gamedescription = value; } }
public string GameImage
{ get { return gameImage; } set { gameImage = value; } }
public string GameGuide
{ get { return gameGuide; } set { gameGuide = value; } }
public string VideoLink
{ get { return videoLink; } set { videoLink = value; } }
}
鉴于我们没有数据,这里唯一显著的区别是
.GameTitle.
与.GameID.
。我假设“不起作用”在这里的意思是“它抛出一个异常”。我可以预测的主要异常是NullReferenceException
,因为.GameID
是null
,所以.GameID。任何都是非法的
这让我觉得无论什么
都不存在于您认为它存在的位置(这将导致.GameID
成为null
引用)。需要核实的事项:
- 它存在于那里
- 案例是否正确(
ID
,ID
,ID
,等等)
- 这是一个元素,而不是属性
- 它不在名称空间中
更新:以下是数据中的问题:
<MaxPayne3>
<Title>Part I Complete - 20G</Title>
<Description>Complete Part I Of The Story</Description>
<Guide>Story related, cannot be missed.</Guide>
<Image>http://www.xbox360achievements.org/images/achievements/925/-K6lMA==.jpg</Image>
<VideoLink/>
</MaxPayne3>
大多数
节点没有子
节点,因此此代码:
select new NewGamesClass
{
GameID = (string)query.Element("ID"),
GameTitle = (string)query.Element("Title"),
GameDescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
}
guide.Where(ngc => ngc.GameID.StartsWith("000"))
将为GameID
生成大部分空值。然后这个代码:
select new NewGamesClass
{
GameID = (string)query.Element("ID"),
GameTitle = (string)query.Element("Title"),
GameDescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
}
guide.Where(ngc => ngc.GameID.StartsWith("000"))
将为所有这些元素抛出NullReferenceException
将其更改为:
guide.Where(ngc => !String.IsNullOrEmpty(ngc.GameID) && ngc.GameID.StartsWith("000"))
它应该可以正常工作。您还没有说它以什么方式不工作。GameID=(string)query.Element(“ID”),-它是string,我得到一个NullReferenceException。抱歉,我还在喝早上的咖啡。是的,我得到了一个空引用异常,但一切似乎都是正确的。@Twenty40按f5。。。我已经解释过了。有很多没有ID/GameID的记录