How to read attribute value from XmlNode in C# ?

How to read attribute value from XmlNode in C# ?

Asked on January 2, 2019 in XML.
Add Comment


  • 3 Answer(s)

    Try the code to read attribute value from XmlNode in C#,

    string employeeName = chldNode.Attributes["Name"].Value;
    
    Answered on January 2, 2019.
    Add Comment

    Try the alternative code below;

    if (node.Attributes != null)
    {
      var nameAttribute = node.Attributes["Name"];
      if (nameAttribute != null)
        return nameAttribute.Value;
     
      throw new InvalidOperationException("Node 'Name' not found.");
    }
    
    Answered on January 2, 2019.
    Add Comment

    The user can able to loop through all attributes like doing with nodes,

    foreach (XmlNode item in node.ChildNodes)
    {
      // node stuff...
     
      foreach (XmlAttribute att in item.Attributes)
      {
        // attribute stuff
      }
    }
    

     

    Answered on January 2, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.