-1

This is my requirement: Write a program that accepts an XML input string and get all node values by name from it. Use the namespace System.XML ( Do not use System.XML.Linq).

Display & node values separated by space.

Example: If input string contains following string:

<Names> <Name> <FirstName>John</FirstName> <LastName>Smith</LastName</Name>
<Name><FirstName>James</FirstName> <LastName>White</LastName></Name></Names>

Display output as : John Smith James White

Create a class named UserProgramCode that has the following static method public static string getnodeByName(string input1) Create a class named Program that accepts the inputs and calls the static method present in the UserProgramCode.

And My code is

using System; using System.Xml;

public class Program{

    public static void Main(){

    string xmlinput=Console.ReadLine();
    UserProgramCode.getnodeByName(xmlinput);
    }

}

public class UserProgramCode{

    static string ResultName;
    public static string getnodeByName(string input1)
    {

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(input1);
        string xpath ="Names/Name";
        var nodes = xmlDoc.SelectNodes(xpath);
        foreach (XmlNode childrenNode in nodes)
        {
              UserProgramCode.ResultName=childrenNode.SelectSingleNode("/FirstName").InnerText +" "+ childrenNode.SelectSingleNode("/LastName").InnerText;
        }
        return UserProgramCode.ResultName;
    }
}

But I am getting Null reference Exception.I have no idea how to resolve this. Please help . I am new to C#.net.

John Smith James White Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at UserProgramCode.getnodeByName (System.String input1) [0x00000] in :0 at Program.Main () [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at UserProgramCode.getnodeByName (System.String input1) [0x00000] in :0 at Program.Main () [0x00000] in :0

  • I'm guessing the null reference exception is because `SelectSingleNode( )` is returning null, and if it does, trying to access `.InnerText` will cause this error. You can prevent it from crashing your program if you use `?.InnerText`, but in your case you need to figure out why `SelectSingleNode()` is returning null. Hint: `/` at the beginning of an XPath behaves similarly to how it does with a filesystem path. – Dylan Nicholson Dec 03 '17 at 08:43
  • There are two issues : 1) You are missing a '>' after last name. 2) You need a period in the path : UserProgramCode.ResultName = childrenNode.SelectSingleNode("./FirstName").InnerText + " " + childrenNode.SelectSingleNode("./LastName").InnerText; – jdweng Dec 03 '17 at 10:07
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Progman Dec 03 '17 at 13:01

1 Answers1

1

I see multiple problems here:

  1. Your Input XML is malformed. You should try catch while LoadXml as a user can enter anything to break your code. Smith</LastName ">" is missing.

  2. / in front of the nodes name /FirstName will make it start search from the root of the current node.

  3. Assigning to ResultName will only keep last node's First and Last Name and discard all others.

  4. Using a string concatenation will result in wasted heap.

  5. Naming conventions don't match C# standards.

Updated sample:

    public class UserProgramCode
    {
     static StringBuilder resultName = new StringBuilder();
     public static string GetNodeByName(string input1)
     {
        XmlDocument xmlDoc = new XmlDocument();
        try
        {
            xmlDoc.LoadXml(input1);
        }
        catch (XmlException xe)
        {
            Console.WriteLine("Input XML is not parseable. " + xe.Message);
        }
        string xpath = "Names/Name";
        var nodes = xmlDoc.SelectNodes(xpath);
        foreach (XmlNode childrenNode in nodes)
        {
            resultName.AppendLine(childrenNode.SelectSingleNode("FirstName").InnerText + " " + childrenNode.SelectSingleNode("LastName").InnerText);
        }
        return resultName.ToString();
     }
    }
Sunil
  • 3,404
  • 10
  • 23
  • 31