2

我的问题的目标是通过 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 时,输出始终是“不存在节点”。

我阅读了几篇基于关键字提取节点的帖子,并且之前建议他们检查命名空间。但我的 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";
    }

}

4

1 回答 1

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 += "&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";
    }
}
于 2013-11-15T02:53:32.600 回答