By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

XmlTextReader - finding attributes and then children.

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.