以下是我的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中执行此操作。
答案 0 :(得分: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");
答案 1 :(得分: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>