asp.net - how to search for a node in a xml file to see it exist -


I'm trying to get an XML file. Fields are always the same but occasionally are not included in it.

How do I find "name" in vb? & lt; Products & gt; & Lt; Name & gt; & Lt; FirstName & gt; John & lt; / FirstName & gt; & Lt; Last name & gt; Do & lt; / LastName & gt; & Lt; / Name & gt; & Lt; Age & gt; 24 & lt; / Age & gt; & Lt; Date of birth & gt; 4/5/1990 & lt; / DOB & gt; & Lt; / Products & gt;

I tried the code below and found a compilation, error BC30456: 'This includes' System.XML.Linq.XElement is not a member of

 If the doctor ("product") as an XDocument in the form of a slow doctor. This includes ("name") then the doctor root.Element ("product") element ("name"). Remove () End if    

The method of testing is available only to access it And then check that it is not nothing , like this:

  If the doctor Element ("product"). Element ("name") isnot 'nothing.   

Note that instead of doc.Element ("product") doc.Root.Element ("product") In your example XML, the product is the basic element, however, it is dangerous to use the element property because it will throw an exception if the original element does not exist In other words, if doc.Element ("product") returns nothing , then you can not go back and can not remove anything . Element ("name") . So, to fix this, you have to do something like this:

  if (Doctor is not Element ("product") and also ( Doctor "Element" ("product"). Element ("name") is not) then '... end if   

but it's ugly very fast. to access the elements. & Lt; & Gt; The easiest way to use, such as:

  if the doctor & Lt; Products & gt; & Lt; Name & gt; .FirstOrDefault IsNot If nothing ... end ... if  nothing  is returned to  product  or  name  it will not throw an exception if Both of them either disappear or both disappear. If you want to throw an exception, you .  syntax, but just call  first  instead of  first and default .  

Of course, I can tell that if you want to use XPath (Industry Standards) instead of Microsoft's proprietary LINQ-to-XML, then all this will be a bit easier, cleaner and more flexible . Technology, but it will be just fine, well, I think it has gone against my good decision, so long as I have already said, I can also give an example:

   

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -