0

以下是我的 XML:

<Security xmlns="http://docs.oasis-open.org/x/xxxxx.xsd" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Assertion ID="xxxxx" IssueInstant="xxxxxxx" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">MyApp</saml:Issuer>
<saml:Subject>
  <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">MyApp</saml:NameID>
</saml:Subject>
<saml:AttributeStatement>
  <saml:Attribute Name="UserID" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml:AttributeValue>TestUserID</saml:AttributeValue>
  </saml:Attribute>
  <saml:Attribute Name="UserFirstName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml:AttributeValue>TestUserFirstName</saml:AttributeValue>
  </saml:Attribute>
  <saml:Attribute Name="UserLastName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml:AttributeValue>TestUserLastName</saml:AttributeValue>
  </saml:Attribute>
  <saml:Attribute Name="ReasonForSearch" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
    <saml:AttributeValue>ReasonForSearch</saml:AttributeValue>
  </saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</Security>

在我的代码中,我想用适当的值替换 TestUserLastName、TestUserFirstName、TestUserID 和 ReasonForSearch。

我试着做这样的事情:

XmlDocument doc = new XmlDocument();
        doc.Load("C:/TFS/xxx/yyy/zzz/SAMLAssert.xml");

        XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
        xmlnsManager.AddNamespace("sa", "saml:Assertion");
        xmlnsManager.AddNamespace("sas", "saml:AttributeStatement");
        xmlnsManager.AddNamespace("satt", "saml:Attribute");
        xmlnsManager.AddNamespace("sattv", "saml:AttributeValue");

        XmlNodeList xnList = doc.SelectNodes("/sa/sas/satt");

xnList计数始终为 0。我很想在 Linq-to-XML 中执行此操作。

4

2 回答 2

0

尝试:

XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("oasis", "http://docs.oasis-open.org/x/xxxxx.xsd");
xmlnsManager.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");

XmlNodeList xnList = doc.SelectNodes("/oasis:Security/saml:Assertion/saml:AttributeStatement/saml:Attribute");
于 2013-09-04T20:40:31.500 回答
0

我很想在 Linq-to-XML 中做到这一点。

给你:

// load document
var xDoc = XDocument.Load("Input.txt");
// prepare XNamespace instance
var saml = XNamespace.Get("urn:oasis:names:tc:SAML:2.0:assertion");

// query for items and put them into Dictionary<string, XElement>
var attributes = xDoc.Root.Element(saml + "Assertion")
                          .Element(saml + "AttributeStatement")
                          .Elements(saml + "Attribute")
                          .ToDictionary(x => (string)x.Attribute("Name"), x => x.Element(saml + "AttributeValue"));

// update XElement values using created Dictionary
attributes["UserID"].Value = "New UserID";
attributes["UserFirstName"].Value = "New UserName";
attributes["UserLastName"].Value = "New UserLastName";
attributes["ReasonForSearch"].Value = "New ReasonForSearch";

// save updated document back
xDoc.Save("Input.txt");

结果 XML:

<Security xmlns="http://docs.oasis-open.org/x/xxxxx.xsd" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
  <Assertion ID="xxxxx" IssueInstant="xxxxxxx" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
    <Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">MyApp</Issuer>
    <Subject>
      <NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified">MyApp</NameID>
    </Subject>
    <AttributeStatement>
      <Attribute Name="UserID" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
        <AttributeValue>New UserID</AttributeValue>
      </Attribute>
      <Attribute Name="UserFirstName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
        <AttributeValue>New UserName</AttributeValue>
      </Attribute>
      <Attribute Name="UserLastName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
        <AttributeValue>New UserLastName</AttributeValue>
      </Attribute>
      <Attribute Name="ReasonForSearch" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
        <AttributeValue>New ReasonForSearch</AttributeValue>
      </Attribute>
    </AttributeStatement>
  </Assertion>
</Security>
于 2013-09-04T20:46:35.290 回答