开发者

Checking for existence of node in Linq to XML

开发者 https://www.devze.com 2023-03-25 19:34 出处:网络
I have a series of XML files I am cycling through in a folder.They are almost always the same, but from time to time I run into a file that does not contain an element the others contain.For example,

I have a series of XML files I am cycling through in a folder. They are almost always the same, but from time to time I run into a file that does not contain an element the others contain. For example, in one file, it will look like this:

<sb_sbgp>
  <itemtitle>French-Canadian</itemtitle>
  <itemtype>subscription</itemtype>
  <audience>French people</audience>
</sb_sbgp>

but the next file may look like this:

<sb_sbgp>
  <itemtitle>Spanish</itemtitle>
  <itemtype>subscription</itemtype>
</sb_sbgp>

my problem is that I can't just pull in those files that contain the audience element (which would allow me to add a wher开发者_JS百科e clause). Ideally, I would want it to default to null if it doesn't exist. Here is the query I'm using to try and accomplish this:

XDocument doc = XDocument.Load(mydoc.xml);
var x = (from node in doc.Descendants("sb_sbgp")
         select new
         {
             title = node.Element("itemtitle").Value,
             type = node.Element("itemtype").Value,
             audience = string.IsNullOrEmpty(node.Element("sb_sbgp_audience").Value) ? node.Element("sb_sbgp_audience").Value : null
         });

I realize that is testing to see if there is a value, not an element, but I can't find anything that would allow me to test for the existence of the element. Help?


try...

XDocument doc = XDocument.Load("");
var x = (from node in doc.Descendants("sb_sbgp")
     select new
     {
         title = node.Element("itemtitle").Value,
         type = node.Element("itemtype").Value,
         audience = (node.Element("sb_sbgp_audience") != null) ? node.Element("sb_sbgp_audience").Value : null
     });


You're in luck - it's actually really easy to do this :)

Instead of using .Value, just cast to string and LINQ to XML will do what you want:

audience = (string) node.Element("sb_sbgp_audience")

If you cast a null XElement or XAttribute reference to any nullable type (whether a reference type or a nullable value type) you'll get a null result. Very handy.

The only downside of this is that if there's an empty element, you'll still get the empty string instead of a null reference. If you really want to avoid this, I suggest creating an extension method like this:

public static string ToNullIfEmpty(this string text)
{
    return string.IsNullOrEmpty(text) ? null : text;
}

then you can use:

audience = ((string) node.Element("sb_sbgp_audience")).ToNullIfEmpty()


How about this:

audience = node.Elements("sb_sbgp_audience").Count() > 0 ? node.Element("sb_sbgp_audience").Value : null


How about this?

XDocument doc = XDocument.Load(mydoc.xml);
var x = (from node in doc.Descendants("sb_sbgp")
         select new
         {
             title = node.Element("itemtitle").Value,
             type = node.Element("itemtype").Value,
             audience = node.Element("audience") != null) ? node.Element("audience").Value : null
         });
0

精彩评论

暂无评论...
验证码 换一张
取 消