Reading XML when no XML present for xmlnode...

Hello:

I load an xml document to read, the documents vary in form and shape from each xml file to the next. I run the following:
rootnode2.SelectSingleNode("//dt_array/item[@key='2']/dt_assoc/item[@key='name']")
It returns the 'name'
When I run the same statement but change the key='3' it throws an error since in the xml document there is not key='3'. But some documents will include the key='3' and some will not, how do I work around this? I tried the following which failed to work...
If rootnode2.SelectSingleNode("//dt_array/item[@key='3']/dt_assoc/item[@key='name']").innertext = "" Then
response.write("blank")
End If
any ideas on how I would detect to see if the xmlnode actually exists before I query it?
Need ASP.NET web hosting? Private Message me for a free quote.
0
WebD
5/18/2004 10:17:46 PM
asp.net.xml-datasource 7181 articles. 0 followers. Follow

5 Replies
837 Views

Similar Articles

[PageSpeed] 29

Use the Count method to check how many elements were found with SelectSingleNode. 

Something like this:

XmlNode node = rootnode2.SelectSingleNode("//dt_array/item[@key='2']/dt_assoc/item[@key='name']")
If(node.Count>0)
// Found something
else
// Nothing Found

Sonu
Sonu Kapoor [MVP]
Feel free to ask me any .NET question | Ajax Data Controls Forum
0
SonuKapoor
5/18/2004 10:38:27 PM
In that code that you gave, the error will throw before it counts the node...
Need ASP.NET web hosting? Private Message me for a free quote.
0
WebD
5/18/2004 10:49:16 PM
Your error must occur somewhere else. I use this method in my code and it works. For example copy/paste this code and run it and you will see that it works.

xml file

<?xml version="1.0" ?>
<root>
<sub>
<name>sonu</name>
</sub>
<sub>
<name>WebD</name>
</sub>
</root>

code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<% @Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e){
if(!Page.IsPostBack){
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("test.xml"));
XmlNodeList nodes = xmlDoc.SelectNodes("root[name='don']");
if(nodes.Count > 0){
Response.Write("Found something");
Response.Write(nodes.Count.ToString());
}
else
Response.Write("Sorry no result");
}
}
</script>


Sonu
Sonu Kapoor [MVP]
Feel free to ask me any .NET question | Ajax Data Controls Forum
0
SonuKapoor
5/18/2004 11:03:39 PM
Thank You,

I tried the following code and it worked fine, based on your example:

dim sreadersuccessx as xmlnodelist

sreadersuccessx = xmldoc2.selectnodes("//dt_array/item[@key='2']/dt_assoc/item[@key='name']")

if sreadersuccessx.count > "0" Then
response.write( sreadersuccessx.count)
end if

I appreciate it!
Need ASP.NET web hosting? Private Message me for a free quote.
0
WebD
5/18/2004 11:41:14 PM
No prob...nice to see that your problem is solved.

Sonu
Sonu Kapoor [MVP]
Feel free to ask me any .NET question | Ajax Data Controls Forum
0
SonuKapoor
5/18/2004 11:49:53 PM
Reply:

Similar Artilces:

XMl to XML
Hello,   I retrieved an XML from the dataset which retrieves data from the table in the database. I need to present the data  in a different structure. Is XSLT the way or are there any other options. please let me know. Thanks!! Yes XSLT is a good choice to convert xml file to other forms. You can take a look at XSLT Tutorial.Sincerely,Young Fang...

XML in, XML out
Hi All (and a happy holiday to those that will get a break), I am trying to read in an XML file of addresses. I need to remove all the address data from the file where code =~ /^000/ (there are none in the example data below). I need to reproduce that data 'as is', so I need to honour the tag structure, although the order of the tags doesn't need to be honoured. I have been trying to use XML::Simple and I had a go with XML::Smart but I haven't been able to get the results I want. My best effort is below (with XML::Simple). There are a couple of differences ...

reading data from xml service or xml datasource
Hi, I'm trying to figure out a way to import data (but not export to, I'm just trying to get their news section) from an external db (SqlServer 2000 or 2005) to a gridview and detailsview on my site (resides at a web host). I spoke to the organisation that owns the external db and they are willing to look into several solutions, whitout giving out their db password etc. Not suprisingly, I was reccomended using a web service. However, I believe I need to install stuff in order to enable one, and so, it won't be able for me (I guess), because of my web host's limitations. Instead, I am now t...

XML::Parser XML::DOM XML::XSLT
------_=_NextPart_001_01C6E0D0.3E27D77A Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Hi Craig and fellow VMS Perlers, You last wrote about this Fri 3/16/2001 11:24 AM, not counting your very helpful comments on Eisner/Decuserve more recently. One of my colleagues sent me an email requesting that I make XML::XSLT=20 available on VMS. perl 5.8.6, VMS 7.3-2, DECC 7.1 latest ACRTL (version 3) on Alpha XML::XSLT (version 0.48) depends on XML::Parser (version 2.34). This depends on expat (version 2.0.0). I built expat a...

XML to XML transform
Hi all, I have an XML file in which I just want to change a couple of values.  So, the end result is a new file with exactly the same XML structure.  I am new to XML and XSLT, so I thought I'd start off with something simple; I created an xsl file that would just copy the source directly across without modifying the values (I can do this later, once I have the first bit figured) - <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:con="http://eviware.com/soapui/config"><xsl:template match="/"...

sort xml in xml
Hi, How I can sort xml file in xml with VB.NetThanks,<?xml version="1.0" encoding="ISO-8859-1" ?>  <Liste>   <Form Famille="Administration" idXhtml="" idOffice="31574" nom="ADM" desc="" />   <Form Famille="Admini" idXhtml="" idOffice="31708" nom="ADM- 54 (2000)" desc="" />   <Form Famille="communication" idXhtml="" idOffice="31709" nom="ADM- 133 (2000)" desc="" />   <Form Famille="Admini" idXhtml="" idOffice="31710" nom="ADM- 9 (2000)" desc="" />   <Form Famille="Admini" idXhtml="" idOffice="31711" no...

xml read Error (while reading xml file with foriegn characters)
------=_Part_71356_21654157.1168239182303 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Guru, I have a problem with reading xml file which has foriegn characters (like plut=F4t) using xml::Simple. I get xml read error. Then I included encoding=3Diso8859-1 at the top of xml file. I am able to read the xml file , but the characters are changed to plut?t instead of plut=F4t, Because of this my test fails. I need to change the file to UTF-8 format. If I include encoding=3D"U...

Transform XML to XML
I've been trying to transform an XML document to another XML document. The original document is very large (6megs) and the application that needs to use it only needs a fraction of the data. I'm trying to take this mother-of-all XML documents and filter out unwanted nodes. I could probably do this very easily with code by running a few XPath Queries against the document, and placing the results in a new document...however...I have an interest in learning how to do this with XSL. In the past, I've done some pretty basic XML to HTML transformation ans that worked out well. This ti...

How to select Xml as Xml?
If I select Xml nodes like this from sql server; select xmlData.query('//Field[@F = "True"]') as extendedInfo from.... ..how to loop through the items in C#? I can't cast it to a System.Xml.XmlNodeList..? You can use the XmlDocument class.  There's a Load method and a LoadXml method.  One of these should get you what you want, after which you can select nodes as you need.C# <---> VB.Net Translator mm8: If I select Xml nodes like this from sql server; select xmlData.query('//Field[@F = "True"]') as extendedInfo from.... ...

New to XML (how to read xml file)
I want to read the following XML file....could you give me the code how to do this....this will help me alot ..<siteMapNode1>    <siteMapNode2 url="url1" title="title1"  description="desc1" />    <siteMapNode2 url="url2" title="title2"  description="desc2" /></siteMapNode1>thanks a lot in advance...........  check this link, it might contain what you are looking for. http://samples.gotdotnet.com/quickstart/howto/doc/Xml/ReadXMLFile.aspx    thanks du...

XML reading from xml file but not from webpage
Hi, The following code works fine when reading from an xml file:    xmlfile = Server.MapPath("xml") & "\Jobs.xml" But when I try to read from an external xml source it gives an error: The given path's format is not supported. xmlfile = Server.MapPath("xml") & http://xxxxxxxxxxxxxxxxxxxxx/ Can anyone help? Thanks. Clare   Server.MapPath maps to local resources. In your case, it maps the physical path to the xml folder and looks for Jobs.xml inside that. To get data from an an external source, take a look at the XmlDocument class, specifically the Load()...

read XML tag in XML file
Is there a way to read the XML tags being sent in an XML file? I can import the data fine, but I need to check for a certain XML tag. ...

XML Transform to 1 XML file to another XML File
Hello All  I am a novice programmer. I want to transform 1 xml file to another xml file. I am looking for a example code where user will select source xml file and create another xml file in different file with different fields with some default validation of parsing date and default values.  Can anyone help me or let me know any available source code in C#/ASP.Net?  Ms Disha I am assuming you know xslt, if not, you would have to go look at that.  But if you do, then this document will help you out http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=...

Load XMl file and read xml file
How do I load and read nodes from the xml file in c#.??? ThankYou Sample code: http://www.kirupa.com/net/reading_xml_directly_pg1.htmJack Yang.NET Developer Here is how you read xml file and loop and display nodesXmlDocument xDoc = new XmlDocument();xDoc.Load(xmlPath); foreach(XmlNode node in xDoc.SelectNodes("//nodeName you want to display")){Response.Write("name: " + node.Attributes["name"].InnerText + " value: " + node.Attributes["value"].InnerText);}...

Web resources about - Reading XML when no XML present for xmlnode... - asp.net.xml-datasource

The Oracle, Reading - Wikipedia, the free encyclopedia
51°27′12″N 0°58′21″W  /  51.45328°N 0.97239°W  / 51.45328; -0.97239 Coordinates : 51°27′12″N 0°58′21″W  /  51.45328°N 0.97239°W  / 51.45328; ...

What Australians are reading this summer, from Magda Szubanski to Bill Bryson
Which books are in everyone's beach bags this summer? We ask leading booksellers to reveal their top sellers.

Mohamed El-Erian: Required reading for an age of global economic uncertainty
With the global economy being jolted, investment superstar Mohamed El-Erian outlines his holiday reading list.

Bad Lip Reading
Swallow your coffee (or beer) first

Weekend Reading: D-Squared Digest: One-Minute MBA: Avoiding Projects Pursued By Morons 101
**Daniel Davies**: [The D-Squared Digest One Minute MBA: Avoiding Projects Pursued By Morons 101](http://blog.danieldavies.com/2004_05_23_d- ...

Michael Ian Black released new memoir 'Navel Gazing,' doing readings/in-stores this week
Comedian and actor Michael Ian Black (The State, Wet Hot American Summer) releases his new memoir, Navel Gazing, this week. It's actually his ...

Reading between the lines at One Bills Drive
Jerry Sullivan says Whaley, Ryan did their best to paint picture of team unity at press conference.

How reading the Bible in a year changed my life
It's hard work but deeply rewarding, says Martin Saunders.

Librarian Turns Under The Stairs Cupboard Into A Harry Potter Inspired Reading Nook
Pennsylvanian librarian Courtney McCarraher Bonnet has transformed the cupboard under the stairs [...]

Recommended Reading: How Taylor Swift took on Apple
Taylor Swift Is the New Prince: The Artist Who Tamed the Corporate GiantGaby Dunn, The Daily BeastIt's true: Taylor Swift went toe to toe with ...

Resources last updated: 1/6/2016 11:15:20 AM