"soupaman" <se********@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.com...
Im trying to output some filtered xml using the xmlTextReader
One approach is to subclass an XmlTextReader with these sorts
of filtering capabilities on attribute values. Putting the logic inside
of a subclass (vs. out in an XML application) greatly simplifies
what your XML consumers need to deal with and gives you a
greater deal of re-use.
- - - AttributeFilterXmlTextReader.cs
using System;
using System.Collections;
using System.Xml;
public class AttributeFilterXmlTextReader : XmlTextReader
{
string tagName;
string attrName;
ArrayList attrValues;
public AttributeFilterXmlTextReader(
TextReader reader,
string tagNameOfInterest,
string attrNameOfInterest,
string[] attrValuesOfInterest ) : base( reader)
{
tagName = base.NameTable.Add( tagNameOfInterest);
attrName = base.NameTable.Add( attrNameOfInterest);
attrValues = new ArrayList( attrValuesOfInterest);
}
public override bool Read( )
{
bool result = base.Read( );
if ( result && (this.NodeType == XmlNodeType.Element))
{
if (this.LocalName == tagName)
{
string candidate = this.GetAttribute( attrName);
if ( null != candidate)
{
if ( !attrValues.Contains( candidate))
{
this.Skip( );
result = this.Read( );
}
}
}
}
return result;
}
}
- - -
what I want it to do is match the filters (an array) and output the
value of fruit so in this case where I pass in the filter(type
attribute) "Apple,Orange.Other" should print "Washington,California -
Granny Smith,[null]"
Given the following XML instance document (which is equivalent to your
example but it has to have one document element),
- - - mydata.xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<produce>
<fruit type="Apple">Washington</fruit>
<fruit type="Banana">Chiquita</fruit>
<fruit type="Orange">Florida</fruit>
<fruit type="Orange.Other">California</fruit>
</produce>
<produce>
<fruit type="Apple">Granny Smith</fruit>
<fruit type="Orange">Navel</fruit>
</produce>
</root>
- - -
Then the following code could be used in an application to load the
filtered XML into an XmlDocument straightaway (for instance in your
Page's PreRender event handler),
// Create a specialized XmlTextReader to filter the instance document.
StreamReader src = File.OpenText( Server.MapPath( "Data//mydata.xml"));
AttributeFilterXmlTextReader afxtr = new AttributeFilterXmlTextReader(
src, "fruit", "type", new string[] { "Apple", "Orange.Other" }
);
// Create an XmlDocument and Load( ) it to drive the Read( ) loop
// around the XmlTextReader so it can do it's filtering thing.
XmlDocument doc = new XmlDocument( );
doc.Load(afxtr);
afxtr.Close( );
// Render as markup (perform necessary translations of '<' so tag names
// are not eaten by the browser, and linefeeds into HTML line-breaks).
Response.Write( doc.OuterXml.Replace( "<", "<").Replace( "\n", "<br />") );
Any help greatly appreciated..if you know of a better way even better!
Another approach you could use is to just load the XML into an
XPathDocument and then query it with XPath expressions or'ing
together the fruit elements of interest. If it's not a large volume of
XML you'll be reading, this may be more easily maintained.
string xpExpr = "/root/produce/fruit[@type='Apple' or @type='Orange.Other']";
XPathDocument xpd = new XPathDocument( Server.MapPath( "Data//mydata.xml"));
XPathNodeIterator nodeSet = xpd.SelectNodes( );
foreach (XPathNavigator nodeNavigator in nodeSet)
{
Response.Write( nodeNavigator.InnerXml + "<br />");
}
XPath expressions can be modified more easily than the logic in an
XmlTextReader subclass, if your requirements may be subject to
change with regard to whether you're performing an intersection
(logical-OR) or union (logical-AND) of filter criteria.
Derek Harmon