我的问题的目标是通过aspx页面从用户获取XML文件的URL和关键字(节点的名称)。
我的XML文件: -
<document-metadata xmlns="http://breeze.macromedia.com/" version="1.0">
<document-info>
<title>Harry Potter</title>
<summary/>
<author/>
<keywords/>
<thumbnail href="data/thumb/thumb_slide_000001.jpg"/>
<view-link href="/Viewer.swf?slide={position}"/>
</document-info>
<section xmlns="" type="slide" position="1">
<title>Part 1</title>
<content>XYZ</content>
<related-content/>
<thumbnail href="data/thumb/thumb_slide_000001.jpg"/>
</section>
<section xmlns="" type="slide" position="2">
<title>Part 2</title>
<content> PQRS</content>
<related-content/>
<thumbnail href="data/thumb/thumb_slide_000002.jpg"/>
</section>
</document-info>
</document-metadata>
我的C#代码: -
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() + " <> ";
//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();
}
我的代码问题在于,当我通过aspx页面提交我的URl XML和Keyword时,输出始终是“No Node Exists。”
我阅读了几篇基于关键字提取节点的帖子,之前他们建议检查命名空间。但我的XML文件总是不一样。该URL将更改用于检查节点的XML文件。
这是我修正的最终代码: -
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 && node.FirstChild.Name != "#text")
{
XmlNodeList childern = node.ChildNodes;
string str = "Child Nodes are:-";
foreach (XmlNode child in childern)
str += "<" + child.Name + ">";
return str;
}
else if ( node.OuterXml!=null && 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";
}
}
答案 0 :(得分:0)
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];
//result = node.LocalName + " - - " + node.Name + " - - " + node.NodeType + " - - " + node.OuterXml;
//return result;
//foreach (XmlNode node in nodes){
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 && node.FirstChild.Name != "#text")
{
XmlNodeList childern = node.ChildNodes;
string str = "Child Nodes are:-";
foreach (XmlNode child in childern)
str += "<" + child.Name + ">";
return str;
}
else if ( node.OuterXml!=null && node.InnerText.ToString() != String.Empty)
{
return node.InnerText.ToString();
}
/*else if (node.OuterXml != "" && node.Value != null)
{
string result;
result = node.LocalName + " - - " + node.Name + " - - " + node.NodeType + " - - " + node.OuterXml;
return result;
}*/
else if (node.ParentNode != null)
return node.ParentNode.Name;
else
return node.Name;
/*
if (node.OuterXml != "")
{
if (node.HasChildNodes && node.FirstChild.Name != "#text")
{
XmlNodeList childern = node.ChildNodes;
string str = "Child Nodes are:-";
foreach (XmlNode child in childern)
str += "<" + child.Name + ">";
return str;
}
else
{
if (node.OuterXml == string.Empty)
return node.ParentNode.Name;
else
return node.OuterXml;
//return "Outer Xml null part";
//return node.OuterXml;
}
}
else
{
//return "Outer Xml null part";
//string result;
// = node.LocalName + " - - " + node.Name + " - - " + node.NodeType + " - - " + node.OuterXml;
//return result;
if (node.ParentNode != null)
{
string str = "Parent Node is :-";
str += node.ParentNode.Name.ToString();
return str;
}
else
return node.Name;
//return node.OuterXml;
}
*/
}
catch(Exception e)
{
return "Error Occured : Try Again with New Input Set";
}
}