Jump to content

c# node select troubles.

Go to solution Solved by DXMember,

Try something like this:


XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

https://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx

So trying to make use of XML, in this case, its a proxy XML from http://www.xroxy.com/proxyrss.xml.

 

but when trying to select the nodes:

 

 XmlDocument doc = new XmlDocument();
doc.Load("http://www.xroxy.com/proxyrss.xml");
XmlNodeList nodelist = doc.SelectNodes("/channel/item/prx:proxy");
foreach(XmlNode node in nodelist)
{
    var ip = node.SelectSingleNode("prx:ip").InnerText;
    var port = node.SelectSingleNode("prx:port").InnerText;
    proxyList.AppendText(ip+":"+port+"\n");
}

I am getting this error

 

System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'
 

I assume it's because of the ':' in the name but I have no idea how to fix it.

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/801278-c-node-select-troubles/
Share on other sites

Link to post
Share on other sites

Try something like this:


XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

https://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
https://linustechtips.com/topic/801278-c-node-select-troubles/#findComment-10076732
Share on other sites

Link to post
Share on other sites

22 minutes ago, DXMember said:

Try something like this:

 


XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

 

https://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx

Thanks solves this with 

 

XmlDocument doc = new XmlDocument();
            doc.Load("http://www.xroxy.com/proxyrss.xml");
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("prx", "http://www.proxyrss.com/content");
            XmlNodeList nodelist;
            XmlElement root = doc.DocumentElement;
            nodelist = root.SelectNodes("//prx:proxy", nsmgr);
            foreach (XmlNode node in nodelist)
            {
                var ip = node.SelectSingleNode("prx:ip", nsmgr).InnerText;
                var port = node.SelectSingleNode("prx:port", nsmgr).InnerText;
                proxyList.AppendText(ip + ":" + port + "\n");
            }

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/801278-c-node-select-troubles/#findComment-10076808
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×