Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

xml check element

have the following working.
but when the "node" has element that does not exist in the xml string, it fails.
How to add validation check if the element node is not existed? then it can just return string.empty?
  public string GetXMLInformation(string node, string xmlString)
        {
            string returnValue = string.Empty;
            #region all fields
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xmlString);
            XmlNodeList tempNodeList = null;
            foreach (XmlNode item in xmlDoc.SelectNodes("//Surety"))
            {
                tempNodeList = item.ChildNodes;
                foreach (XmlNode tempNode in tempNodeList)
                {
                    if (tempNode.Name == node)
                    {
                        returnValue = tempNode.InnerText;
                    }
                }
            }
            #endregion
            return returnValue;
        }

Open in new window

Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

any helps
Avatar of ste5an
It's not clear what you're trying to solve here, but what does "fails" mean?

If the node content does not exists as element, then the result is string.Empty. If the content of xmlString is invalid, then the procedure throws an exception.

But searching for the nodes content is much simple than using loops:

public bool ReadNodeContents(string xmlString, string node, out nodeContent string)
{
    bool result = false;
    XmlDocument document = new XmlDocument();
    document.LoadXml(xmlString);
    nodeContent = string.Empty;
    foreach (XmlNode item in document.SelectNodes("//Surety/" + node))
    {
        nodeContent += item.InnerText;
    }

    return result;
}

Open in new window

failed - mean the node element i am looking for does not exist
Yup, Minor typo, just wrote that down as an outline...

namespace ConsoleCS
{
    using System;
    using System.Xml;

    public class Program
    {
        public static void Main(string[] args)
        {
            bool result;
            string nodeContents;
            result = ReadNodeContents("<Surety><element>value</element></Surety>", "element", out nodeContents);
            Console.WriteLine($"Result: {result}; Node contents: '{nodeContents}'");

            result = ReadNodeContents("<Surety><element>value</element></Surety>", "anotherElement", out nodeContents);
            Console.WriteLine($"Result: {result}; Node contents: '{nodeContents}'");

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }

        public static bool ReadNodeContents(string xmlString, string node, out string nodeContents )
        {
            bool result = false;
            XmlDocument document = new XmlDocument();
            document.LoadXml(xmlString);
            nodeContents = string.Empty;
            foreach (XmlNode item in document.SelectNodes("//Surety/" + node))
            {
                nodeContents += item.InnerText;
                result = true;
            }

            return result;
        }
    }
}

Open in new window

Obviously, we need to set the result value to true in the loops body.
But my example asks for return string not bool
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial