Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > XmlTextReader - finding attributes and then children.

Reply
Thread Tools

XmlTextReader - finding attributes and then children.

 
 
lejason@gmail.com
Guest
Posts: n/a
 
      08-27-2007
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>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      08-28-2007
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


"(E-Mail Removed)" 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>
>
>

 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      08-28-2007
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)

http://www.velocityreviews.com/forums/(E-Mail Removed) 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>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xmltextreader and character & CindyH ASP .Net 1 05-20-2008 10:28 PM
reading attributes with no quotes using XmlTextReader apiringmvp ASP .Net 3 11-28-2006 05:56 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
XMLTextReader is not defined =?Utf-8?B?WE1MIHJlYWRpbmcgd2l0aCBYTUxUZXh0UmVhZGVy?= ASP .Net 2 01-26-2005 01:29 PM
XmlTextReader DotNet ASP .Net 1 02-06-2004 06:44 PM



Advertisments