ASP.NET Razor Pages: Create HTML Representing Structure of Xml -


i trying display data in xml(~/server_settings.xml):

<?xml version="1.0" encoding="utf-8"?> <site>     <general>         <name>venture corp</name>         <url>http://anerdsplayground.com/</url>         <developer>otard95</developer>     </general>     <ore_contracts>         <use_system description="this system id ore contract app gets prices from">30000142</use_system>     </ore_contracts> </site> 

on site. want give style css , add form input each node has text value, can later edit content.

i want able read new nodes well, file going expand in future. needs read dynamic xml.

i'm using code:

@using system.xml @using system.xml.linq @using system.xml.xpath  @functions{      xmldocument doc = null;     string output = "";      xmldocument set_doc{         set{             doc = value;         }     }      string write_xml(string path){          // add start tag , attributes node may have         output += "<div ";          if(doc.selectsinglenode(path).attributes.count > 0){             // description attribute added p tag save later             string desc = "";              // loop through attributes             foreach(xmlattribute att in doc.selectsinglenode(path).attributes){                 if(att.name == "description"){ // if description attribute exists save later                     desc = att.innertext;                 }else{ // write attribute output                     output += att.name + '=' + '"' + att.innertext + '"' + ' ';                 }             }             // close starting tag             output += ">";              // if node has description attribute add <p> tag             if(desc != ""){                 output += "<p>" + desc + "</p><br/>";             }         }          // if current node has 1 or more child nodes execute function each of them         if(doc.selectsinglenode(path).haschildnodes){             foreach(xmlnode next_node in doc.selectsinglenode(path).childnodes){                 string new_path = path + "/" + next_node.name; // create path next element                 write_xml(new_path); // write next inner node             }              // close node             output += "</div>";         }else{ // no inner nodes add content of current node , close element             output += "<input type=" + '"' + "text" + '"' + " name=" + '"' + "node_content" + '"' + " value=" + '"' + doc.selectsinglenode(path).innertext + '"' + " />";         }         return output;     } }  @{     // xml eve-centeral current ore-prices , creat dictionary     xmldocument doc = new xmldocument();     doc.load(server.mappath("~/server_settings.xml"));     string root = "/site";      set_doc = doc; }  <!doctype html>  <html lang="en">     <head>         <meta charset="utf-8" />         <title></title>         <style>          </style>     </head>     <body>         @html.raw(write_xml(root));     </body> </html> 

but error:

server error in '/' application.  expression must evaluate node-set.  description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.   exception details: system.xml.xpath.xpathexception: expression must evaluate node-set.  source error:    line 19:         output += "<div "; line 20:  line 21: error-->if(doc.selectsinglenode(path).attributes.count > 0){ line 22:             // description attribute added p tag save later line 23:             string desc = "";  line: 21   stack trace:    [xpathexception: expression must evaluate node-set.]    ms.internal.xml.xpath.xpathparser.parsenodetest(astnode qyinput, axistype axistype, xpathnodetype nodetype) +5596737    ms.internal.xml.xpath.xpathparser.parsestep(astnode qyinput) +75    ms.internal.xml.xpath.xpathparser.parserelativelocationpath(astnode qyinput) +18    ms.internal.xml.xpath.xpathparser.parselocationpath(astnode qyinput) +118    ms.internal.xml.xpath.xpathparser.parsepathexpr(astnode qyinput) +30    ms.internal.xml.xpath.xpathparser.parseunionexpr(astnode qyinput) +19    ms.internal.xml.xpath.xpathparser.parseunaryexpr(astnode qyinput) +44    ms.internal.xml.xpath.xpathparser.parsemultiplicativeexpr(astnode qyinput) +21    ms.internal.xml.xpath.xpathparser.parseadditiveexpr(astnode qyinput) +21    ms.internal.xml.xpath.xpathparser.parserelationalexpr(astnode qyinput) +21    ms.internal.xml.xpath.xpathparser.parseequalityexpr(astnode qyinput) +21    ms.internal.xml.xpath.xpathparser.parseandexpr(astnode qyinput) +19    ms.internal.xml.xpath.xpathparser.parseorexpr(astnode qyinput) +19    ms.internal.xml.xpath.xpathparser.parseexpresion(astnode qyinput) +30    ms.internal.xml.xpath.xpathparser.parsexpathexpresion(string xpathexpresion) +54    system.xml.xpath.xpathexpression.compile(string xpath, ixmlnamespaceresolver nsresolver) +51    system.xml.xpath.xpathnavigator.select(string xpath) +14    system.xml.xmlnode.selectnodes(string xpath) +45    system.xml.xmlnode.selectsinglenode(string xpath) +7    asp._page_administrative_edit_settings_cshtml.write_xml(string path) in c:\users\otard95\google drive\personal\websites\venturecorpdrive\administrative\edit_settings.cshtml:21    asp._page_administrative_edit_settings_cshtml.write_xml(string path) in c:\users\otard95\google drive\personal\websites\venturecorpdrive\administrative\edit_settings.cshtml:46    asp._page_administrative_edit_settings_cshtml.write_xml(string path) in c:\users\otard95\google drive\personal\websites\venturecorpdrive\administrative\edit_settings.cshtml:46    asp._page_administrative_edit_settings_cshtml.write_xml(string path) in c:\users\otard95\google drive\personal\websites\venturecorpdrive\administrative\edit_settings.cshtml:46    asp._page_administrative_edit_settings_cshtml.execute() in c:\users\otard95\google drive\personal\websites\venturecorpdrive\administrative\edit_settings.cshtml:78    system.web.webpages.webpagebase.executepagehierarchy() +197    system.web.webpages.webpage.executepagehierarchy(ienumerable`1 executors) +68    system.web.webpages.webpage.executepagehierarchy() +151    system.web.webpages.webpagebase.executepagehierarchy(webpagecontext pagecontext, textwriter writer, webpagerenderingbase startpage) +76    system.web.webpages.webpagehttphandler.processrequestinternal(httpcontextbase httpcontext) +114 

desired output (example):

<html lang="en">     <body>         <div>nodename:             <div attributes xml node might have>                 <p>value of attribute named "description"</p>                 <form method="post">                     <input type="text" name="node_content" value="current content of xml node"/>                 </form>             </div>         </div>          <div attributes xml node might have>             <p>value of attribute named "description"</p>             <form method="post">                 <input type="text" name="node_content" value="current content of xml node"/>             </form>         </div>          ...      </body> </html> 

i'm quite new system.xml namespace might not best way it. have not found tutorial, choice has been use method of trial , error. if decide reply please explain little. , if have tutorial handy, link maybe, i'd appreciate it.

if missed , need more info, please give me heads-up , i'll edit post can.

thanks in advance might provide!

as @har07 suggested found value of path variable when exception occurs, , got /site/general/name/#text. did not know text content of xmlelement considered childnode

solution:

if(doc.selectsinglenode(path).haschildnodes && doc.selectsinglenode(path).firstchild.gettype().tostring() != "system.xml.xmltext"){     foreach(xmlnode next_node in doc.selectsinglenode(path).childnodes){         string new_path = path + "/" + next_node.name; // create path next element         write_xml(new_path); // write next inner node     }      // close node     output += "</div>"; }else{ 

edit:

added && doc.selectsinglenode(path).firstchild.gettype().tostring() != "system.xml.xmltext" if statement

many @har07


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -