我问题的目标是通过aspx页面从用户获取XML文件的URL和关键字(Node的名称)。
我的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页面提交XML和关键字的URl时,输出总是“Node不存在”。
我读了很少的文章,基于关键字提取节点,之前他们被建议检查名称空间。但是我的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";
}}
发布于 2013-11-15 02:53:32
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";
}
}https://stackoverflow.com/questions/19821432
复制相似问题