Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何做到这一点,它确实显示了;“没有价值”;而不是陷入错误_C#_Visual Studio 2010_C# 4.0_Silverlight 4.0 - Fatal编程技术网

C# 如何做到这一点,它确实显示了;“没有价值”;而不是陷入错误

C# 如何做到这一点,它确实显示了;“没有价值”;而不是陷入错误,c#,visual-studio-2010,c#-4.0,silverlight-4.0,C#,Visual Studio 2010,C# 4.0,Silverlight 4.0,我正在使用下面的代码,只要我没有缺少一个子节点(例如URL),它就可以正常工作。如果是 丢失,然后我的代码出错。我怎样才能使它不出错,而只返回一个字符串“No Value” 这是我的密码 string widgetsInfo = loaded.Descendants("widget") .Select((w, i) => new { Widg

我正在使用下面的代码,只要我没有缺少一个子节点(例如URL),它就可以正常工作。如果是 丢失,然后我的代码出错。我怎样才能使它不出错,而只返回一个字符串“No Value”

这是我的密码

string widgetsInfo = 
    loaded.Descendants("widget")
          .Select((w, i) =>
                new
                    {
                        WidgetIndex = i,
                        URL = w.Descendants("url").FirstOrDefault().Value,
                        Category = w.Descendants("PortalCategoryId").FirstOrDefault().Value
                    })
            .Select(w => String.Format("Index:{0}; URL:{1}; CATEGORY:{2}; ",
                                        w.WidgetIndex, w.URL, w.Category))
            .Aggregate((acc, next) => acc + Environment.NewLine + next);
下面是我正在解析的xml

string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<widgets>
    <widget>
        <url>~/Portal/Widgets/ServicesList.ascx</url>
        <castAs>ServicesWidget</castAs>
        <urlType>ascx</urlType>
        <parameters>
            <PortalCategoryId>3</PortalCategoryId>
        </parameters>
    </widget>
    <widget>
        <url>www.omegacoder.com</url>
        <castAs>ServicesWidget</castAs>
        <urlType>htm</urlType>
        <parameters>
            <PortalCategoryId>41</PortalCategoryId>
        </parameters>
    </widget>
</widgets>";
stringxml=@”
~/Portal/Widgets/ServicesList.ascx
服务区
户控件
3.
www.omegacoder.com
服务区
热媒
41
";

试试linq子句。

@user400749哪个节点?类别?只需重复我用于“url”的相同逻辑。请参阅我的编辑。否它不适用于任何…如果节点丢失,它将抛出错误…类别或url
   string widgetsInfo = 
    loaded.Descendants("widget")
          .Select((w, i) =>
                new
                    {
                        WidgetIndex = i,
                        URL = w.Descendants("url").FirstOrDefault() == null ? "No Value" : w.Descendants("url").FirstOrDefault().Value,
                        Category = w.Descendants("PortalCategoryId").FirstOrDefault() == null ? "No Value" : w.Descendants("PortalCategoryId").FirstOrDefault().Value
                    })
            .Select(w => String.Format("Index:{0}; URL:{1}; CATEGORY:{2}; ",
                                        w.WidgetIndex, w.URL, w.Category))
            .Aggregate((acc, next) => acc + Environment.NewLine + next);