Assign string value to InnerText XML attribute value

I am trying to extract the first word from a string into the firstName element. All remaining words should go in the lastName element.

Example

ClientName = Stev Finance Company

Here Stev is the firstName and Finance Company is the lastName.

Here is my code, where doc is an XML document:

// XML construction – no issue here 
XmlDocument Mainroot = new XmlDocument();
XmlElement root = Mainroot.CreateElement("Parent");
XmlElement firstName = Mainroot.CreateElement("FirstName");
XmlElement lastName = Mainroot.CreateElement("LastName");

var clientname = XmlHelper.getString(doc, "//BusinessClient/ClientName"); 
var firstName = clientname.Split(' ');
var lastName = clientname.Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);

firstName.InnerText = firstName; // Getting an error: "Cannot Convert string[] to string"
lastName.InnerText = lastName; // Getting an error: "Cannot Convert string[] to string"

Please let me know why I am getting an error.

3 answers

  • answered 2021-01-18 17:06 TinoZ

    When you are replacing var with the explicit type you can see that firstName is string... and String doen't have a innerText Property but the provided code looks incomplete, because you should get an error if that block is all in one method because of double instatiation.

  • answered 2021-01-18 17:10 Yojizan

    Because, 'Split' method returns array of strings. If you want to get only one string, try this: var firstName = clientname.Split(' ').FirstOrDefault(); var lastName = clientname.Split(' ', (char)StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

    https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=net-5.0

  • answered 2021-01-18 17:21 D M

    The other answers are correct; you have two issues here:

    1. You are reusing the firstName identifier for an XmlElement and a string array returned from the string.Split() method.
    2. You are attempting to assign a string array to the XmlElement.innerText property, but it expects a string.

    To fix these issues, rename or inline one of the variables and change the type of the values you are assigning to string instead of string[]. You can achieve this by using string.Join() to concatenate the values in the string array back into a string. In the example below, the values are joined with a space and the first word is skipped (since it was used as the first name).

    // XML construction – no issue here 
    XmlDocument Mainroot = new XmlDocument();
    XmlElement root = Mainroot.CreateElement("Parent");
    XmlElement firstName = Mainroot.CreateElement("FirstName");
    XmlElement lastName = Mainroot.CreateElement("LastName");
    
    var clientname = XmlHelper.getString(doc, "//BusinessClient/ClientName"); 
    
    // Set the value of this element to the first word in the client name.
    firstName.innerText = clientname.Split(' ').FirstOrDefault();
    
    // Set the value of this element to the rest of the word(s) in the client name.
    lastName.innerText = string.Join(" ", clientname.Split(' ', (char)StringSplitOptions.RemoveEmptyEntries).Skip(1));