Random photo
Loading...
Domains for sale
|
August 17, 2003Document-free XPath compilerTherefore obvious solutions are: using dummy XmlDocument or XPathDocument object to get XPathNavigator and make use of its Compile() method or implement dummy XPathNavigator class. Dummy object vs dummy implementation, hehe. Well, dummy implementation at least doesn't allocate memory, so I'm advocating this solution. Below is the implementation and its usage:
public sealed class XPathCompiler {
private sealed class DummyXpathNavigator : XPathNavigator {
public override XPathNavigator Clone() {
return new DummyXpathNavigator();
}
public override XPathNodeType NodeType {
get { return XPathNodeType.Root; }
}
public override string LocalName {
get { return String.Empty; }
}
public override string NamespaceURI {
get { return String.Empty; }
}
public override string Name {
get { return String.Empty; }
}
public override string Prefix {
get { return String.Empty; }
}
public override string Value {
get { return String.Empty; }
}
public override string BaseURI {
get { return String.Empty; }
}
public override String XmlLang {
get { return String.Empty; }
}
public override bool IsEmptyElement {
get { return false; }
}
public override XmlNameTable NameTable {
get { return null; }
}
public override bool HasAttributes {
get { return false; }
}
public override string GetAttribute(string localName,
string namespaceURI) {
return string.Empty;
}
public override bool MoveToAttribute(string localName,
string namespaceURI) {
return false;
}
public override bool MoveToFirstAttribute() {
return false;
}
public override bool MoveToNextAttribute() {
return false;
}
public override string GetNamespace(string name) {
return string.Empty;
}
public override bool MoveToNamespace(string name) {
return false;
}
public override bool MoveToFirstNamespace(XPathNamespaceScope
namespaceScope) {
return false;
}
public override bool MoveToNextNamespace(XPathNamespaceScope
namespaceScope) {
return false;
}
public override bool HasChildren {
get { return false; }
}
public override bool MoveToNext() {
return false;
}
public override bool MoveToPrevious() {
return false;
}
public override bool MoveToFirst() {
return false;
}
public override bool MoveToFirstChild() {
return false;
}
public override bool MoveToParent() {
return false;
}
public override void MoveToRoot() {}
public override bool MoveTo( XPathNavigator other ) {
return false;
}
public override bool MoveToId(string id) {
return false;
}
public override bool IsSamePosition(XPathNavigator other) {
return false;
}
public override XPathNodeIterator SelectDescendants(string name,
string namespaceURI, bool matchSelf) {
return null;
}
public override XPathNodeIterator SelectChildren(string name,
string namespaceURI) {
return null;
}
public override XPathNodeIterator
SelectChildren(XPathNodeType nodeType) {
return null;
}
public override XmlNodeOrder
ComparePosition(XPathNavigator navigator) {
return new XmlNodeOrder();
}
}
private static XPathNavigator _nav =
new DummyXpathNavigator();
public static XPathExpression Compile(string xpath) {
return _nav.Compile(xpath);
}
}
public class XPathCompilerTest {
static void Main(string[] args) {
//Document-free compilation
XPathExpression xe = XPathCompiler.Compile("/foo");
//Usage of the compiled expression
XPathDocument doc =
new XPathDocument(new StringReader("<foo/>"));
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator ni = nav.Select(xe);
while (ni.MoveNext()) {
Console.WriteLine(ni.Current.Name);
}
}
}
August 17, 2003 12:28 PM
| #.NET
Comments
Vlad, always call MoveNext before accessing XPathNodeIterator. Posted by: Oleg Tkachenko at May 25, 2006 10:43 PMHow write a node in xsl using C# next code is not working. Thanks,
function fragmentToText(nd){ Well, may be you are right. May be I'm too performance concerned ;) allocates 79 Kb: The bigger memory hog here is in XmlCharType class, which preallocates static 65536 bytes array for all allowed in XML characters. Actually as it's static it might be allocated already, so empty XmlDocument is really cheap in memory terms. Well, if you actually look at the XmlDocument ctor, the allocation sequence is: - new XmlImplementation() next, its CreateNavigator() method, only creates a DocumentXPathNavigator() and stores a couple references (nothing "newed", except an XmlAttribute. That makes up only 3 extra objects, which are pretty much empty, so I really don't see the big issue here. Posted by: Daniel Cazzulino at August 19, 2003 7:59 PMPost a comment
|