[xml] InnerXml functions



Hello!

Due to the superior nature of libxml2/libxslt/libexslt, I am using it via a
C# wrapper rather than using System.XML for a project I am working on...

I realize that by doing my work in C# rather than in one of the supported
bindings that it clouds exactly what coding would be used, but I believe my 

I have written a simple C# Xpath function which returns the InnerXml of an
Xpath query on an xml document supplied in the encoding supplied (see below
- singlenodeinnerxml). I am glad to say that it works, but believe that
perhaps there is room for improvement mainly due to my not fully
understanding the libxml2 API as of yet....

If I have some xml in string myxml such as:

...
<topnode>
<anode>
<bnode>sjdlkdsf</bnode>
<cnode>lkjlkdsf</cnode>
</anode>
<tnode>
<unode>sjsddlkdsf</unode>
<vnode>lkjlkfddsf</vnode>
</tnode>
</topnode>
...

and call singlenodeinnerxml(true, myxml, "//anode",
System.Text.Encoding.ASCII) it returns:

<bnode>sjdlkdsf</bnode>
<cnode>lkjlkdsf</cnode>

I would appreciate any and all guidance.

Best Regards,

Alex


[STAThread]
public static unsafe string singlenodeinnerxml(bool SourceIsString,string
sourcexml, string XPathExpression, System.Text.Encoding xmlencoding)
{
        xmlInitParser();
        _xmlDoc *doc;
        if (SourceIsString == true)
        {
                doc = xmlParseMemory(new
System.Text.StringBuilder(sourcexml),sourcexml.Length);
        }
        else
        {
                doc = xmlParseFile(sourcexml);
        }
        if (doc == null)
        {
                return "";
        }
        _xmlXPathContext *xpathCtx = xmlXPathNewContext(doc);
        if(xpathCtx == null) 
        {
                xmlFreeDoc(doc);
                return "";
        }
        _xmlXPathObject *xpathObj = xmlXPathEval(XPathExpression ,xpathCtx);
        if(xpathObj == null || xpathObj->nodesetval == null)
        {
                xmlXPathFreeContext(xpathCtx);
                xmlFreeDoc(doc);
                return "";
        }
        Console.WriteLine("\n[" + xpathObj->type1.ToString() + "]\n");
        _xmlBuffer *mybuffer = xmlBufferCreate();
        Console.WriteLine("\n[" + xmlNodeDump(mybuffer, doc,
xpathObj->nodesetval->nodeTab[0], 0, 0).ToString() + "] bytes written\n");
        byte *mybyte = xmlBufferContent(mybuffer);
        int xpathlen = xmlStrlen(mybyte);
        byte[] bytesArray = new byte[xpathlen];
        for (int i = 0; i < xpathlen; i ++)
        {
                bytesArray[i] = *(mybyte + i);
        }
        xmlBufferFree(mybuffer);
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx);
        xmlFreeDoc(doc);
        string mystring = xmlencoding.GetString(bytesArray);
        // mystring contains .outerXml
        // the below string manipulations convert .outerXml into .innerXml,
but the design is poor...
        mystring = mystring.Substring(0,mystring.LastIndexOf("<")); //
remove closing outerelement
        mystring = mystring.Substring(mystring.IndexOf(">") + 1); // remove
starting outerelement
        System.GC.Collect();
        System.Windows.Forms.Application.DoEvents();
        return mystring;
}






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]