XmlTextReader - finding attributes and then children.

L

lejason

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>
 
G

Guest

Howdy,

Very easy to do if you use XmlDocument in conjunction with XPath expression:

XmlElement element = null;
XmlDocument document = new XmlDocument();
XmlAttribute attribute = null;

document.Load(Server.MapPath("~/products.xml"));

foreach (XmlNode node in
document.SelectNodes("/productlist/product[@id=002]"))
{
// id
attribute = node.Attributes["id"];
if (attribute != null)
{
string id = attribute.Value;
}

// model
element = node["model"];
if (element != null)
{
string model = element.InnerText;
}

// specs
element = node["specs"];
List<string> specs = new List<string>();

if (element != null)
{
foreach (XmlNode spec in element.ChildNodes)
{
specs.Add(spec.InnerText);
}
}

}

You should be fine from this point.

HTH
--
Milosz


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>
 
B

bruce barker

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)

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top