locked
Store Attribute to string

    Question

  • Hello,

    I have been trying to store an xml attribute value to a string and I cannot seem to get it to work.  I start off pulling an XML node from a SharePoint web service.
    XmlNode user = usergrpService.GetUserInfo("domain\\user");
    This seems to work because if I assign user.InnerXml to a text box then I can see the contents of the user.  So then I try to get the value.

    I have tried a combination of the following with /'s, @'s, User/ID, User@ID, /User/ID, /User@ID, etc. 
    String userID = user.Attributes["ID"].Value;
    String userID = user.SelectSingleNode("ID").Value;
    Each time I get an "Object reference not set to an instance of an object" error.  If I can read the InnerXml then why can I not get a single attribute?

    -Eric


    Thursday, February 25, 2010 7:19 PM

Answers

All replies

  • Can you make sure the attribute ID does exist on the XmlNode user? Because if it does not, then user.Attributes["ID"] will return a null. So you get a null ref exception when you try to access the Value (in user.Attributes["ID"].Value). You can add a check to make sure that is not null.

    If you want to know what all attributes exist on the XmlNode user, you can go through all the attributes returned by user.Attributes and finding the attribute you need.
    Thursday, February 25, 2010 8:44 PM
  • Thank you for the response Varun,

    I did verify that ID was in the node.  I was actually checking the existance and case when I pulled the InnerXml, since I know xml is also case sensitive.

    The good news is that I finally got it to work.  I ended up adding .ChildNodes[0] in the query, so the full line of code looked like this.

    string userID = userNode.ChildNodes[0].SelectSingleNode("@ID").Value;
    or
    string userID = userNode.ChildNodes[0].Attributes["ID"].Value;

     

    The bad news is that I still don't understand it.  It looks like I am pulling an array of child nodes when the object I have (userNode) is supposed to be a single XmlNode.  I understand each attribute is a node as well, but it seems to me that the whole <User ... /> element that I got would be considered the root node in this case, and that .Attributes["ID"].Value would pull the named attrubute value from the single <User> node from the current instance of the object.  I might understand this more if I pulled all users, but I already narrowed it down to one when I pulled the user from the web service.

    I'm still pretty new to this though, so hopefully the clould clears eventually.

    Thursday, February 25, 2010 11:43 PM
  • If you look at the doc for GetUserInfo operation, it returns a GetUserInfo element. The first child of the GetUserInfo element is the User element, which is the one you want to get an attribute from.

    Monday, March 1, 2010 10:30 PM
    Moderator
  • Can you make sure the attribute ID does exist on the XmlNode user? Because if it does not, then user.Attributes["ID"] will return a null. So you get a null ref exception when you try to access the Value (in user.Attributes["ID"].Value). You can add a vob check to make sure that is not null.

    If you want to know what all attributes exist on the XmlNode user, you can go through all the attributes returned by user.Attributes and finding the attribute you need.

    Any examples for reference? I still wonder why though I already got it solved, Thanks for your help!
    Saturday, August 28, 2010 4:13 AM