Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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# Element Exist的c LINQ到XML查询表达式?_C#_Linq To Xml_Elements - Fatal编程技术网

C# Element Exist的c LINQ到XML查询表达式?

C# Element Exist的c LINQ到XML查询表达式?,c#,linq-to-xml,elements,C#,Linq To Xml,Elements,在尝试获取元素pubDate或其他元素之前,我们如何在此处进行检查,因为如果不进行检查,将引发空引用异常???我个人的偏好是向XElement添加两种扩展方法: <channel> <title>Best Web Gallery - Flash + CSS Gallery</title> <link>http://bestwebgallery.com</link> <descript

在尝试获取元素pubDate或其他元素之前,我们如何在此处进行检查,因为如果不进行检查,将引发空引用异常???

我个人的偏好是向XElement添加两种扩展方法:

<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>
        <pubDate>09 Dec 2009</pubDate>    
        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>   


<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>

            // pubDate missing

        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>




   XDocument rssFeed = XDocument.Load(url);

                    var feedResources = from details in rssFeed.Descendants("channel")
                                    select new feedResource
                                    {
                                         Title = details.Element("title").Value,
                                         Host = details.Element("link").Value,
                                         Description = details.Element("description").Value,  

                                         PublishedOn = DateTime.Parse(details.Element("pubDate").Value), 
                                         Generator = details.Element("generator").Value,
                                         Language = details.Element("language").Value
                                    };
现在,您的代码将类似于:

public static string ValueOrDefault(this XElement xml)
{
    if (xml == null) return null;   // or String.Empty, if you prefer
    return xml.Value
}

public static string ValueOrDefault(this XElement xml, string defaultValue)
{
    if (xml == null) return defaultValue;
    return xml.Value
}

我个人倾向于向XElement添加两种扩展方法:

<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>
        <pubDate>09 Dec 2009</pubDate>    
        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>   


<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>

            // pubDate missing

        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>




   XDocument rssFeed = XDocument.Load(url);

                    var feedResources = from details in rssFeed.Descendants("channel")
                                    select new feedResource
                                    {
                                         Title = details.Element("title").Value,
                                         Host = details.Element("link").Value,
                                         Description = details.Element("description").Value,  

                                         PublishedOn = DateTime.Parse(details.Element("pubDate").Value), 
                                         Generator = details.Element("generator").Value,
                                         Language = details.Element("language").Value
                                    };
现在,您的代码将类似于:

public static string ValueOrDefault(this XElement xml)
{
    if (xml == null) return null;   // or String.Empty, if you prefer
    return xml.Value
}

public static string ValueOrDefault(this XElement xml, string defaultValue)
{
    if (xml == null) return defaultValue;
    return xml.Value
}
不要使用解析等;xml通常使用不同于它所接受的字符串表示形式;只需投注编号值:

XElement有转换运算符来完成所有工作,返回适当的值。

不要使用Parse等;xml通常使用不同于它所接受的字符串表示形式;只需投注编号值:

XElement有转换运算符来完成所有工作,并返回适当的值。

只需更改行:

select new FeedResource
{
    Title = (string)details.Element("title"),
    Host = (string)details.Element("link"),
    Description = (string)details.Element("description"),
    PublishedOn = (DateTime?)details.Element("pubDate"),
    Generator = (string)details.Element("generator"),
    Language = (string)details.Element("language")
}
用于:

您可以更改日期时间。现在,无论您想要什么,只需更改行:

select new FeedResource
{
    Title = (string)details.Element("title"),
    Host = (string)details.Element("link"),
    Description = (string)details.Element("description"),
    PublishedOn = (DateTime?)details.Element("pubDate"),
    Generator = (string)details.Element("generator"),
    Language = (string)details.Element("language")
}
用于:

您可以更改日期时间。现在,您可以随意更改