Note that there are some explanatory texts on larger screens.

plurals
  1. POExtract a Node from XML based on KeyWord
    primarykey
    data
    text
    <p>The goal of my question is to take the URL of XML file and a Keyword (Name of a Node) from the user through a aspx Page.</p> <ol> <li>If node has a Value then print the Value.</li> <li>If node has child elements then print the set of Child Elements.</li> <li>If node is Leaf Node print the Parent Node.</li> </ol> <p>My XML File:-</p> <pre><code> &lt;document-metadata xmlns="http://breeze.macromedia.com/" version="1.0"&gt; &lt;document-info&gt; &lt;title&gt;Harry Potter&lt;/title&gt; &lt;summary/&gt; &lt;author/&gt; &lt;keywords/&gt; &lt;thumbnail href="data/thumb/thumb_slide_000001.jpg"/&gt; &lt;view-link href="/Viewer.swf?slide={position}"/&gt; &lt;/document-info&gt; &lt;section xmlns="" type="slide" position="1"&gt; &lt;title&gt;Part 1&lt;/title&gt; &lt;content&gt;XYZ&lt;/content&gt; &lt;related-content/&gt; &lt;thumbnail href="data/thumb/thumb_slide_000001.jpg"/&gt; &lt;/section&gt; &lt;section xmlns="" type="slide" position="2"&gt; &lt;title&gt;Part 2&lt;/title&gt; &lt;content&gt; PQRS&lt;/content&gt; &lt;related-content/&gt; &lt;thumbnail href="data/thumb/thumb_slide_000002.jpg"/&gt; &lt;/section&gt; &lt;/document-info&gt; &lt;/document-metadata&gt; </code></pre> <p>My C# Code:-</p> <pre><code>public string XmlNodeFind(string xmlUrl, string keyword) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(xmlUrl); try { XmlNodeList nodes = xdoc.GetElementsByTagName(keyword); string result = ""; foreach (XmlNode node in nodes) result = OutputNode(node); return result; } catch(Exception e) { return "No Node Exists"; } } public string OutputNode(XmlNode node) { try { if (node.Value == null) { if (node.HasChildNodes) { XmlNodeList childern = node.ChildNodes; string str = ""; foreach (XmlNode child in childern) str = str + child.Name.ToString() + " &lt;&gt; "; //OutputNode(child); } else if (node.ParentNode != null) { return node.ParentNode.Name.ToString(); } else { return node.Name.ToString(); } } else { return node.Value.ToString(); } } catch(Exception e) { return "Error Occured"; } return node.Value.ToString(); } </code></pre> <p>The Problem with my code is that when I submit my URl of XML and Keyword through an aspx page the output is always "No Node Exists."</p> <p>I read few posts ton extracting nodes based on Keywords and prior they were advises to check namespace. But my XML file is not the same always. The URL will change the XML file being used to check the node.</p> <p>THIS IS FINAL CODE THAT I FIXED:-</p> <pre><code>public string XmlNodeFind(string xmlUrl, string keyword) { XmlDocument xdoc = new XmlDocument(); xdoc.Load(xmlUrl); XmlNodeList nodes = xdoc.GetElementsByTagName(keyword); string result = ""; XmlNode node = nodes[0]; if (node != null) { result = OutputNode(node); return result; } else return "Node Does Not Exist !!! Try with a Valid Node."; } public string OutputNode(XmlNode node) { try { if (node.HasChildNodes &amp;&amp; node.FirstChild.Name != "#text") { XmlNodeList childern = node.ChildNodes; string str = "Child Nodes are:-"; foreach (XmlNode child in childern) str += "&amp;lt;" + child.Name + "&amp;gt;"; return str; } else if ( node.OuterXml!=null &amp;&amp; node.InnerText.ToString() != String.Empty) return node.InnerText.ToString(); else if (node.ParentNode != null) return node.ParentNode.Name; else return node.Name; } catch(Exception e) { return "Error Occured : Try Again with New Input Set"; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload