开发者

How to rewrite code that navigates node list

开发者 https://www.devze.com 2023-01-29 06:15 出处:网络
I have just written some code, which as i was writing i thought, this is going to be a nice generic method for searching for a particular node. When i finished i actually realised it was a mess :D

I have just written some code, which as i was writing i thought, this is going to be a nice generic method for searching for a particular node. When i finished i actually realised it was a mess :D

public String sqlReading(String fileName, String path, String nodeId )
{
    XmlDocument doc开发者_JS百科 = new XmlDocument();
    doc.Load(fileName);

    XmlNodeList names = doc.SelectNodes(path);
    foreach (XmlNode xmlDocSearchTerm in names)
    {
        //if the attribute of the node i start at is the same as where i am now
        if (xmlDocSearchTerm.Attributes.Item(0).Value.ToString().Equals(nodeId))
        {
            //get a list of all of its child nodes
            XmlNodeList childNodes = xmlDocSearchTerm.ChildNodes;

            foreach (XmlNode node in childNodes)
            {
                //if there is a node in here called gui display, go inside
                if (node.Name.Equals("GUIDisplay"))
                {
                    XmlNodeList list = node.ChildNodes;
                    //find the sqlsearchstring tag inside of here
                    foreach (XmlNode finalNode in list)
                    {
                        if (finalNode.Name.Equals("sqlSearchString"))
                        {
                            return node.InnerText;
                        }
                    }
                }
            }
        }
    }
    return "";
}

What i intended to do was based on a path - i would start and check to see if the element had the id i was looking for, if it did then i wanted to get inside there and not stop going until i got to the sqlsearchstring tag which was buried two levels deeper. I have managed that, but the issue here is that now i seem to have almost hardcoded a path to the tag opposed to looping there. How could i change my code to stop me from doing this?

Its from the second foreach where its going wrong imo.

Thanks


Haven't tested it but I believe something like this would work, by using a xpath. However I'm not sure the name of the attribute, or is it always the first attribute?

public String sqlReading(String fileName, String path, String nodeId)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);

    XmlNode foundNode = doc.SelectNodes(path).SelectSingleNode("*[@id='" + nodeId + "']/GUIDisplay/sqlSearchString");
    if (foundNode != null)
        return foundNode.InnerText;
    return string.Empty;

}


Im not sure if this is exaclty right (as I dont have an XML document to try it with, but something similar should work

var innerTexts = XDocument.Load(fileName)
    .Elements(path)
    .Where(n => n.Attributes().ElementAt(0).Value == nodeId)
    .SelectMany(n => n.Elements())
    .Where(n => n.Name == "GUIDisplay")
    .SelectMany(n => n.Elements())
    .Where(n => n.Name == "sqlSearchString")
    .Select(n => n.ToString());


I would say recursion is a safe bet (for iterating through nested child nodes) Though, from what I gather, the structure remains the same. And with that in mind, why not use [XmlDocumentObj].SelectSingleNode("/[nodeId='"+nodeId+"']") (or some facsimile) instead? This has the ability to search by attribute name, unless the XML structure is always changed and you never have constant tag (in which case XPath is probably a good idea).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号