gpt4 book ai didi

c# - 根据KeyWord从XML中提取一个节点

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:03 24 4
gpt4 key购买 nike

我的问题的目标是通过 aspx 页面从用户那里获取 XML 文件的 URL 和关键字(节点名称)。

  1. 如果节点有值,则打印该值。
  2. 如果节点有子元素,则打印子元素集。
  3. 如果节点是叶节点打印父节点。

我的 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 时,输出总是“No Node Exists”。

我读了一些关于根据关键字提取节点的帖子,之前他们被建议检查 namespace 。但是我的 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 += "&lt;" + child.Name + "&gt;";
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";
}

最佳答案

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 += "&lt;" + child.Name + "&gt;";
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 += "&lt;" + child.Name + "&gt;";
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";
}
}

关于c# - 根据KeyWord从XML中提取一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com