Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi i have the xml similar to below
and each xml can have multiple IRO nodes under IRP node.

<irp>

<irn n="name1">


<t s="selected1">
<t s="selected13">




<irn n="name2">


<t s="selected6">
<t s="selected8">



now i am trying to get the value of "s" attribute which is in "T" element and will load in to collection, but i nned to get the attribute s value only for the names matching to IRO. n attribute


XML
<pre lang="XML"><?xml version="1.0" encoding="UTF-8"?>

-<IR>


-<IRP comprised="True" n="EFPLANT-SC-2">


-<IRO n="U1-ABV -13820ACT" cd="Schema document version">


-<P i="True" dn="Construction status 2" pd="ConstructionStatus2">

<T v="Recalibrate" s="LastIn"/>

<T v="Recalibrate" s="ELE"/>

<T v="New" s="IIC"/>

<T v="New" s="PID"/>

</P>


-<P i="True" dn="Construction status" pd="ConstructionStatus">

<T v="Existing" s="LastIn"/>

<T v="Existing" s="ELE"/>

<T v="New" s="IIC"/>

<T v="New" s="PID"/>

</P>


-<P i="False" dn="Instrument sequence in loop" pd="InstSequenceInLoop">

<T v="0" s="LastIn"/>

<T v="0" s="ELE"/>

<T v="0" s="IIC"/>

</P>


-<P i="False" dn="X-coincidence factor" pd="XCoincidenceFactor">

<T v="0" s="LastIn"/>

<T v="0" s="ELE"/>

</P>


-<P i="False" dn="Y-coincidence factor" pd="YCoincidenceFactor">

<T v="0" s="LastIn"/>

<T v="0" s="ELE"/>

</P>



What I have tried:

Private Function ProcessFileToKnowToolsWhichHavePropPublishedInDomain(ByVal pstrPath As String, ByVal pstrname As String) As List(Of String)

Dim lcolDomainwhichhaveatleastoneproperty As New List(Of String)
Dim lvalidateDOMReply As New XmlDocument
lvalidateDOMReply.Load(pstrPath)
Dim xml As XElement = XElement.Load(pstrPath)
'Dim userNodes As XmlNodeList = lvalidateDOMReply.SelectNodes("IR/IRP/IRO[@n=" + pstrname + "]")
Dim myNodes As IEnumerable(Of XElement) = xml.Descendants("IRP").Descendants("IRO").Where(Function(x) x.Attribute("n").Value = pstrname)
'lvalidateDOMReply.

For Each lobjPropertyNode As XElement In myNodes
Dim tool As String = (lobjPropertyNode.Elements("s").Value).ToString()
If Not lcolDomainwhichhaveatleastoneproperty.Contains(tool) Then lcolDomainwhichhaveatleastoneproperty.Add(tool)
Next

Return lcolDomainwhichhaveatleastoneproperty
End Function


i HAVE TRIED ABOVE CODE but getting the object refernce exception.not able to find wher ethe value is nothing please suggest
Posted
Updated 8-Jun-16 9:07am
Comments
Sergey Alexandrovich Kryukov 8-Jun-16 14:02pm    
System.Xml.Linq.XDocument is more suitable for such things, as it supports LINQ to XML.
As to the exception, just use the debugger. You did not even comment the line where the exception is thrown...
—SA
Member 12573064 8-Jun-16 14:14pm    
Hi SA,
Thanks for the reply
It has thrown the exception in the for each loop at
Dim Tool As string

Please see my comment to the question. Better use System.Xml.Linq.XDocument more suitable for such things as it supports LINQ to XML:
XDocument Class (System.Xml.Linq),
LINQ to XML.

Member 12573064 wrote:

It has thrown the exception in the for each loop at
Dim Tool As string
As to your exception, it looks like you have a real stopper here. Look at this line:
VB
Dim tool As String = (lobjPropertyNode.Elements("s").Value).ToString()

If you are sure that this is the line where the exception is thrown, it can be only if lobjPropertyNode.Elements("s") returns null, and then you try to dereference this object, assuming it is not null, by taking its member .Value. At this point, the exception is thrown.

And then you probably do wrong thing. Instead of inspecting the expressions under the debugger, you start guessing and asking questions which you should answer, not anyone else. You cannot do it when you face such situations; it's very important to learn dealing with them all by yourself.

Not to worry. This is one of the easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
Comments
Richard Deeming 8-Jun-16 15:25pm    
Based on the code in the question, he's already using XLinq. It's slightly confused by the fact that he's loading the XML twice - once into an XmlDocument and once into an XElement - but the loop is using the XElement.
Sergey Alexandrovich Kryukov 8-Jun-16 17:09pm    
Yes, it is confusing. Why System.XML.XmlDocument? My point is different: I suggested to use System.Xml.Linq.XDocument instead of System.XML.XmlDocument.
—SA
You are selecting the matching IRO nodes, and then trying to read the element "s" from those nodes. Based on the XML you've posted, the IRO nodes do not have a child element called "s".

If you want to read the attribute from the T elements, you need to select those elements, and read the attribute:
VB.NET
Dim myNodes As IEnumerable(Of XElement) = xml
    .Elements("IRP")
    .Elements("IRO")
    .Where(Function(x) x.Attribute("n").Value = pstrname)
    .Elements("P")
    .Elements("T")

For Each lobjPropertyNode As XElement In myNodes
    Dim tool As String = lobjPropertyNode.Attribute("s").Value
    ...
Next
 
Share this answer
 
Comments
Member 12573064 9-Jun-16 0:23am    
Thanks Richard it worked for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900