1

I am trying to add values to the saml:AttributeValue elements of this xml document:

<saml:AttributeStatement  xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Attribute Name="FNAME">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="LNAME">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="Gender">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="UniqueID">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="DateOfBirth">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>
<saml:Attribute Name="ClientID">
  <saml:AttributeValue></saml:AttributeValue>
</saml:Attribute>

Using this c# code:

//get the AttributeStatement node
    XmlNode attrs = assertion.SelectSingleNode("//saml:AttributeStatement", ns1);
//get the Attribute nodes within the AttributeStatement node
            XmlNodeList attr = attrs.SelectNodes("//saml:Attribute", ns1);

//foreach node in the Attribute node list get the AttributeValue node and add an innerText value
            foreach (XmlNode xn in attr)
            {
                XmlNode attrValue = xn.SelectSingleNode("//saml:AttributeValue", ns1);
                switch (xn.Attributes["Name"].Value)
                {
                    case "FNAME":
                        attrValue.InnerText = UserInfo.FirstName;
                        break;
                    case "LNAME":
                        attrValue.InnerText = UserInfo.LastName;
                        break;
                    case "Gender":
                        attrValue.InnerText = UserInfo.Email;
                        break;
                    case "UniqueID":
                        attrValue.InnerText = UserInfo.UserID.ToString();
                        break;
                    case "DateOfBirth":
                        attrValue.InnerText = UserInfo.UserID.ToString();
                        break;
                    case "ClientID":
                        attrValue.InnerText = UserInfo.UserID.ToString();
                        break;
                    default:
                        attrValue.InnerText = "No attribute listed";
                        break;
                }
//output each AttributeValue innerText.
                lblTest.Text += attrValue.InnerText + " ";

            }

The lblTest is displaying how I would expect - with the correct values for all 6 elements, but the document is not displaying any values at all. Is this the correct way to loop through and add these values to nodes?

1
  • If your xml is in a file then you need to call assertion.Save(filename). If it's a string xml then you need to create a writer and write to it. Commented Jun 26, 2013 at 22:27

1 Answer 1

0

You are doing the right thing up to a point: the trouble is, you're only updating the in-memory copy of your XML document.

See http://support.microsoft.com/kb/301233 for instructions on how to use XmlDocument.Save() to save your in-memory changes down to your XML file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.