Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Using xmldocument.SelectNodes [Thread Next] Re: Using xmldocument.SelectNodesTo: NULL Date: 4/4/2006 6:33:00 PM
JoeKatz@g... wrote:
> This is all being done in .NET. I'm using an xmldocument and I load
> XML into it that i received back from a web service. Below is a
> "section" of that chunk of xml. Now for each section i use the
> xmld.selectnodes("/xmlhistory/violation") to create an xmlnodelist for
> all the chunks that are "violation" nodes. Once I have that into an
> xmlnodelist , i iterate through them using for each xmlnode in
> xmlnodelist. Inside this for loop i'm gathering the values of each tag
> and creating a nice html table that will later be stored. Now once
> inside this for loop i'm running into an issue. In some cases this xml
> below will have additional fields such as <disposition> but sometimes
> it won't. How can I check while in this for loop if the tag of
> <disposition> is present.
SelectNodes and SelectSingleNode is not only a method of the XML
document (XmlDocument) object but also of any node so for that node in
your for each loop you can call e.g. (C#)
XmlNodeList dispositions = node.SelectNodes("disposition");
and process those elements with another loop or of course, if you expect
only one disposition element or are interested only in the first
disposition element then e.g.
XmlElement disposition = node.SelectSingleNode("disposition") as
XmlElement;
if (disposition != null) {
// now use element node here
}
else {
// do what you want to do if there is no dispositon
}
> If it is I want to ( _disposition =
> xmlnode.Item("disposition").InnerText ) grab the value , if its not I
> want to ignore that tag all together.
That is another possibility the .NET DOM offers, indexing by name e.g.
if (node["disposition"] != null) {
// use node["disposition"].InnerText
}
If you use VB.NET then it should be alike
If Not node.Item("disposition") Is Nothing Then
' use node.Item("disposition").InnerText
End If
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
