XmlTextReader - finding attributes and then children.

Discussion in 'ASP .Net' started by lejason@gmail.com, Aug 27, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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


    "" 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>
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Aug 28, 2007
    #2
    1. Advertising

  3. bruce barker Guest

    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)

    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>
    >
    bruce barker, Aug 28, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DotNet

    XmlTextReader

    DotNet, Feb 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Chris Jackson
    Feb 6, 2004
  2. =?Utf-8?B?WE1MIHJlYWRpbmcgd2l0aCBYTUxUZXh0UmVhZGVy

    XMLTextReader is not defined

    =?Utf-8?B?WE1MIHJlYWRpbmcgd2l0aCBYTUxUZXh0UmVhZGVy, Jan 26, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,037
    William F. Robertson, Jr.
    Jan 26, 2005
  3. apiringmvp
    Replies:
    3
    Views:
    560
    Rad [Visual C# MVP]
    Nov 28, 2006
  4. CindyH

    xmltextreader and character &

    CindyH, May 20, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    347
    bruce barker
    May 20, 2008
  5. Replies:
    0
    Views:
    259
Loading...

Share This Page