473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XmlTextReader - finding attributes and then children.

Hi,

I have an XML file that is a list of product models and info. The
list will eventually contain about 100 products, each product having
multiple elements for things like names, specifications,
images...etc. Here is the XML Doc.

<productlist>
<product id="001">
<name>Articulating Boom</name>
<model>Z-40N</model>
<description><![CDATA[ VARIOUS HTML DATA GOES HERE ]]></description>
<specs>
<spec1>SOMETHING</spec1>
<spec2>SOMETHING 2</spec2>
<spec3>SOMETHING 3</spec3>
</specs>
<images>
<img1>none</img1>
<img2>none</img2>
<img3>none</img3>
</images>
<specsheets>
<pdf1>something.pdf</pdf1>
<pdf2>something2.pdf</pdf2>
<pdf3>something3.pdf</pdf3>
</specsheets>
</product>

<product id="002">
<name>Articulating Boom</name>
<model>Z-40N</model>
<description><![CDATA[ VARIOUS HTML DATA GOES HERE ]]></description>
<specs>
<spec1>SOMETHING</spec1>
<spec2>SOMETHING 2</spec2>
<spec3>SOMETHING 3</spec3>
</specs>
<images>
<img1>none</img1>
<img2>none</img2>
<img3>none</img3>
</images>
<specsheets>
<pdf1>something.pdf</pdf1>
<pdf2>something2.pdf</pdf2>
<pdf3>something3.pdf</pdf3>
</specsheets>
</product>

So my question is, how do I navigate to show only the children of the
product that i want selected. For example, if I have a link for
product 002, I want to be able to select the "id" attribute under
"product" where the attribute value is 002, and then only be showing
data for THOSE children.

I have this code that I found and it works fine for showing all the
elements with said name, but again, I want to filter the elements so
that I am only talking to the elements who's parent "product" element
has the attribute value of 002.

<%@ Page language="c#" %>
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
content.Text = ReadXML("description");
}

public string ReadXML(string thisInfo)
{
System.Text.StringBuilder sbuilder = new
System.Text.StringBuilder();
XmlTextReader reader = null;
try
{
// load the file from the URL
reader = new XmlTextReader("http://rd3247/NET/product.xml");
object productData = reader.NameTable.Add(thisInfo);

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(productData))
{
sbuilder.Append(reader.ReadString());
}
}
}
return sbuilder.ToString();
}
catch(Exception e)
{
Response.Write(e.Message);
return "";
}
finally
{
if (reader!=null)
reader.Close();
}
}
</script>

Aug 27 '07 #1
1 2023
while (reader.Read())
{
// scan for desired product node

if (reader.NodeType == XmlNodeType.Element
&& reader.Name == "product"
&& reader.GetAttribute("id") == "001")
{
if (!reader.IsEmptyElement)
{
int depth = reader.Depth;
for (reader.Read();
reader.Depth != depth;
reader.Read())
{
// process sub nodes
}
}
}
}

-- bruce (sqlwork.com)

le*****@gmail.com wrote:
Hi,

I have an XML file that is a list of product models and info. The
list will eventually contain about 100 products, each product having
multiple elements for things like names, specifications,
images...etc. Here is the XML Doc.

<productlist>
<product id="001">
<name>Articulating Boom</name>
<model>Z-40N</model>
<description><![CDATA[ VARIOUS HTML DATA GOES HERE ]]></description>
<specs>
<spec1>SOMETHING</spec1>
<spec2>SOMETHING 2</spec2>
<spec3>SOMETHING 3</spec3>
</specs>
<images>
<img1>none</img1>
<img2>none</img2>
<img3>none</img3>
</images>
<specsheets>
<pdf1>something.pdf</pdf1>
<pdf2>something2.pdf</pdf2>
<pdf3>something3.pdf</pdf3>
</specsheets>
</product>

<product id="002">
<name>Articulating Boom</name>
<model>Z-40N</model>
<description><![CDATA[ VARIOUS HTML DATA GOES HERE ]]></description>
<specs>
<spec1>SOMETHING</spec1>
<spec2>SOMETHING 2</spec2>
<spec3>SOMETHING 3</spec3>
</specs>
<images>
<img1>none</img1>
<img2>none</img2>
<img3>none</img3>
</images>
<specsheets>
<pdf1>something.pdf</pdf1>
<pdf2>something2.pdf</pdf2>
<pdf3>something3.pdf</pdf3>
</specsheets>
</product>

So my question is, how do I navigate to show only the children of the
product that i want selected. For example, if I have a link for
product 002, I want to be able to select the "id" attribute under
"product" where the attribute value is 002, and then only be showing
data for THOSE children.

I have this code that I found and it works fine for showing all the
elements with said name, but again, I want to filter the elements so
that I am only talking to the elements who's parent "product" element
has the attribute value of 002.

<%@ Page language="c#" %>
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
content.Text = ReadXML("description");
}

public string ReadXML(string thisInfo)
{
System.Text.StringBuilder sbuilder = new
System.Text.StringBuilder();
XmlTextReader reader = null;
try
{
// load the file from the URL
reader = new XmlTextReader("http://rd3247/NET/product.xml");
object productData = reader.NameTable.Add(thisInfo);

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(productData))
{
sbuilder.Append(reader.ReadString());
}
}
}
return sbuilder.ToString();
}
catch(Exception e)
{
Response.Write(e.Message);
return "";
}
finally
{
if (reader!=null)
reader.Close();
}
}
</script>
Aug 28 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Meir S. | last post by:
I think the following is a bug in XmlTextReader: I need to process large XMLs, that are typically constructed of many small elements nested in the root element. Each inner element represents a...
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
5
by: Geoff Bennett | last post by:
While parsing an XML document, my TextReader instance skips nodes. For example, in this fragment: <Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14"> <RelatedEntityRef...
1
by: Lee Atkinson | last post by:
Hi - I am trying to read an XML file using XmlTextReader and write it out using XmlTextWriter. The XML is XHTML. At the moment, I am trying to create a straightforward copy of a file. The XHTML...
1
by: Gustaf | last post by:
My docs have attributes called "order", with a default value of 1. So if there is no "order" attribute, the program shall use the value 1. Implementing this with XmlTextReader was harder than...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
3
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue....
2
by: Rasheed | last post by:
Hi , I am using XmlTextReader in C# code, I have one problem while reading xml file with "XmlTextReader" class. i.e I have traverse all attributs/child node of the xmlnode, After that, I want to...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.