c# - Pulling string from xml -
the xml coming url , need pull string "n0014e1" it. not sure why code not working. put try block around , "data root level invalid"
xml:
<obj is="c2g:network " xsi:schemalocation="http://obix.org/ns/schema/1.0/obi/xsd" href="http://192.168.2.230/obix/config/"> <ref name="n0014e1" is="c2g:local c2g:node"xsi:schemalocation="http://obix.org/ns/sc/1.0/obix/xsd" href="n0014e1/"></ref> </obj>
c# code:
public static string nodepath = "http://" + mainclass.ipaddress + obixpath; public static void xmldata() { xmldocument nodevalue = new xmldocument(); nodevalue.loadxml(nodepath); var nodes = nodevalue.selectnodes(nodepath); foreach (xmlnode node in nodes) { httpcontext.current.response.write(node.selectsinglenode("//ref name").value); console.writeline(node.value); } //console.writeline(node); console.readline(); }
your selectnodes
, selectsinglenode
commands incorrect. both expect xpath string identify node.
try following
string xml = @"<obj is=""c2g:network "" href=""http://192.168.2.230/obix/config/""><ref name=""n0014e1"" is=""c2g:local c2g:node"" href=""n0014e1/""></ref></obj>"; xmldocument nodevalue = new xmldocument(); nodevalue.loadxml(xml); xmlnode r = nodevalue.selectsinglenode("//ref[@name]"); if (r != null) { system.diagnostics.debug.writeline(r.attributes["name"].value); }
also, note, loadxml
method loads xml string; not load remote url.
as @kevintdiy has pointed out xml not entirely correct. in sample above have stripped out xsi
reference lacking definition it.
if have access source xml, either remove reference xsi
if not required or add definition root node.
if not possible, may want consider using regular expression or other string based methods getting value.
Comments
Post a Comment