SecurityElement クラス
アセンブリ: mscorlib (mscorlib.dll 内)


このクラスは、一般 XML オブジェクト モデルとして使用するのではなく、セキュリティ システム内で使用するための単純な XML オブジェクト モデルの簡易実装を目的としています。このドキュメントでは、XML の基本的な知識があることを前提にしています。
要素に対する単純な XML オブジェクト モデルは、次に示す部分から構成されます。
属性ベースの XML 表記を使用して、セキュリティ要素とその値を表現することを強くお勧めします。つまり、要素のプロパティを属性として表現し、プロパティ値を属性値として表現します。タグ内のテキストは入れ子にしないでください。任意の <tag>text</tag> 表現で、通常、<tag value=”text”/> 型の表現を利用できます。属性ベースの XML 表記を使用すると、読みやすくなり、生成される XML シリアル化を簡単に WMI に移植できます。
属性の名前は、1 文字以上にする必要があり、null 参照 (Visual Basic では Nothing) にはできません。要素ベースの値形式を使用する場合、null 参照 (Visual Basic では Nothing) テキスト文字列の要素は、<tag/> 形式で表現します。それ以外の場合、テキストは <tag> トークンと </tag> トークンで区切ります。どちらの書式も、存在する場合に表示される属性と結合できます。
要素のタグ、属性、およびテキストが存在する場合は、常に大文字と小文字が区別されます。XML 書式には、必要に応じて引用符とエスケープ文字が含まれます。XML での使用が無効な文字を含む文字列値を使用すると、ArgumentException が発生します。これらの規則は、すべてのプロパティとメソッドに適用されます。
![]() |
---|
パフォーマンス上の理由から、要素を XML テキスト書式にエンコードする場合にだけ文字の有効性を確認しますが、プロパティまたはメソッドのすべてのセットでは確認しません。静的メソッドを使用すると、必要に応じて明示的に確認できます。 |

SecurityElement クラスのメンバの使用例を次に示します。
Imports System Imports System.Security Imports System.Collections Class SecurityElementMembers <STAThread()> _ Shared Sub Main(ByVal args() As String) Dim xmlRootElement As New SecurityElement("RootTag", "XML security tree") AddAttribute(xmlRootElement, "creationdate", DateTime.Now.ToString()) AddChildElement(xmlRootElement, "destroytime", DateTime.Now.AddSeconds(1.0).ToString()) Dim windowsRoleElement As New SecurityElement("WindowsMembership.WindowsRole") windowsRoleElement.AddAttribute("version", "1.00") ' Add a child element and a creationdate attribute. AddChildElement(windowsRoleElement, "BabyElement", "This is a child element") AddAttribute(windowsRoleElement, "creationdate", DateTime.Now.ToString()) xmlRootElement.AddChild(windowsRoleElement) CompareAttributes(xmlRootElement, "creationdate") ConvertToHashTable(xmlRootElement) DisplaySummary(xmlRootElement) ' Determine if the security element is too old to keep. xmlRootElement = DestroyTree(xmlRootElement) If Not (xmlRootElement Is Nothing) Then Dim elementInXml As String = xmlRootElement.ToString() Console.WriteLine(elementInXml) End If Console.WriteLine("This sample completed successfully; " + "press Enter to exit.") Console.ReadLine() End Sub 'Main ' Add an attribute to the specified security element. Private Shared Function AddAttribute(ByVal xmlElement As SecurityElement, ByVal attributeName As String, ByVal attributeValue As String) As SecurityElement If Not (xmlElement Is Nothing) Then ' Verify that the attribute name and value are valid XML formats. If SecurityElement.IsValidAttributeName(attributeName) AndAlso SecurityElement.IsValidAttributeValue(attributeValue) Then ' Add the attribute to the security element. xmlElement.AddAttribute(attributeName, attributeValue) End If Return xmlElement End Function 'AddAttribute ' Add a child element to the specified security element. Private Shared Function AddChildElement(ByVal parentElement As SecurityElement, ByVal tagName As String, ByVal tagText As String) As SecurityElement If Not (parentElement Is Nothing) Then ' Ensure that the tag text is in valid XML format. If Not SecurityElement.IsValidText(tagText) Then ' Replace invalid text with valid XML text ' to enforce proper XML formatting. tagText = SecurityElement.Escape(tagText) ' Determine whether the tag is in valid XML format. If SecurityElement.IsValidTag(tagName) Then Dim childElement As SecurityElement childElement = parentElement.SearchForChildByTag(tagName) If Not (childElement Is Nothing) Then Dim elementText As String elementText = parentElement.SearchForTextOfTag(tagName) If Not elementText.Equals(tagText) Then ' Add child element to the parent security element. parentElement.AddChild(New SecurityElement(tagName, tagText)) End If Else ' Add child element to the parent security element. parentElement.AddChild(New SecurityElement(tagName, tagText)) End If End If End If Return parentElement End Function 'AddChildElement ' Create and display a summary sentence ' about the specified security element. Private Shared Sub DisplaySummary(ByVal xmlElement As SecurityElement) ' Retrieve tag name for the security element. Dim xmlTreeName As String = xmlElement.Tag.ToString() ' Retrieve tag text for the security element. Dim xmlTreeDescription As String = xmlElement.Text ' Retrieve value of the creationdate attribute. Dim xmlCreationDate As String = xmlElement.Attribute("creationdate") ' Retrieve the number of children under the security element. Dim childrenCount As String = xmlElement.Children.Count.ToString() Dim outputMessage As String = "The security XML tree named " + xmlTreeName outputMessage += "(" + xmlTreeDescription + ")" outputMessage += " was created on " + xmlCreationDate + " and " outputMessage += "contains " + childrenCount + " child elements." Console.WriteLine(outputMessage) End Sub 'DisplaySummary ' Compare the first two occurrences of an attribute ' in the specified security element. Private Shared Sub CompareAttributes(ByVal xmlElement As SecurityElement, ByVal attributeName As String) ' Create a hash table containing the security element's attributes. Dim attributeKeys As Hashtable = xmlElement.Attributes Dim attributeValue As String = attributeKeys(attributeName).ToString() Dim xmlChild As SecurityElement For Each xmlChild In xmlElement.Children If attributeValue.Equals(xmlChild.Attribute(attributeName)) Then End If Next xmlChild ' The security elements were created at the exact same time. End Sub 'CompareAttributes ' Convert the contents of the specified security element ' to hash codes stored in a hash table. Private Shared Sub ConvertToHashTable(ByVal xmlElement As SecurityElement) ' Create a hash table to hold hash codes of the security elements. Dim xmlAsHash As New Hashtable() Dim rootIndex As Integer = xmlElement.GetHashCode() xmlAsHash.Add(rootIndex, "root") Dim parentNum As Integer = 0 Dim xmlParent As SecurityElement For Each xmlParent In xmlElement.Children parentNum += 1 xmlAsHash.Add(xmlParent.GetHashCode(), "parent" + parentNum.ToString()) If Not (xmlParent.Children Is Nothing) AndAlso xmlParent.Children.Count > 0 Then Dim childNum As Integer = 0 Dim xmlChild As SecurityElement For Each xmlChild In xmlParent.Children childNum += 1 xmlAsHash.Add(xmlChild.GetHashCode(), "child" + childNum.ToString()) Next xmlChild End If Next xmlParent End Sub 'ConvertToHashTable ' Delete the specified security element if the current time is past ' the time stored in the destroytime tag. Private Shared Function DestroyTree(ByVal xmlElement As SecurityElement) As SecurityElement Dim localXmlElement As SecurityElement = xmlElement Dim destroyElement As SecurityElement = localXmlElement.SearchForChildByTag("destroytime") ' Verify that a destroytime tag exists. If Not (localXmlElement.SearchForChildByTag("destroytime") Is Nothing) Then ' Retrieve the destroytime text to get the time ' the tree can be destroyed. Dim storedDestroyTime As String = localXmlElement.SearchForTextOfTag("destroytime") Dim destroyTime As DateTime = DateTime.Parse(storedDestroyTime) If DateTime.Now > destroyTime Then localXmlElement = Nothing Console.WriteLine("The XML security tree has been deleted.") End If End If ' Verify that xmlElement is of type SecurityElement. If xmlElement.GetType().Equals(GetType(System.Security.SecurityElement)) Then ' Determine whether the localXmlElement object ' differs from xmlElement. If xmlElement.Equals(localXmlElement) Then ' Verify that the tags, attributes and children of the ' two security elements are identical. If xmlElement.Equal(localXmlElement) Then ' Return the original security element. Return xmlElement End If End If End If ' Return the modified security element. Return localXmlElement End Function 'DestroyTree End Class 'SecurityElementMembers ' ' This sample produces the following output: ' ' The security XML tree named RootTag(XML security tree) ' was created on 2/23/2004 1:23:00 PM and contains 2 child elements. '<RootTag creationdate="2/23/2004 1:23:00 PM">XML security tree ' <destroytime>2/23/2004 1:23:01 PM</destroytime> ' <WindowsMembership.WindowsRole version="1.00" ' creationdate="2/23/2004 1:23:00 PM"> ' <BabyElement>This is a child element.</BabyElement> ' </WindowsMembership.WindowsRole> '</RootTag> ' 'This sample completed successfully; press Exit to continue.
using System; using System.Security; using System.Collections; class SecurityElementMembers { [STAThread] static void Main(string[] args) { SecurityElement xmlRootElement = new SecurityElement("RootTag", "XML security tree"); AddAttribute(xmlRootElement,"creationdate",DateTime.Now.ToString()); AddChildElement(xmlRootElement,"destroytime", DateTime.Now.AddSeconds(1.0).ToString()); SecurityElement windowsRoleElement = new SecurityElement("WindowsMembership.WindowsRole"); windowsRoleElement.AddAttribute("version","1.00"); // Add a child element and a creationdate attribute. AddChildElement(windowsRoleElement,"BabyElement", "This is a child element"); AddAttribute(windowsRoleElement,"creationdate", DateTime.Now.ToString()); xmlRootElement.AddChild(windowsRoleElement); CompareAttributes(xmlRootElement, "creationdate"); ConvertToHashTable(xmlRootElement); DisplaySummary(xmlRootElement); // Determine if the security element is too old to keep. xmlRootElement = DestroyTree(xmlRootElement); if (xmlRootElement != null) { string elementInXml = xmlRootElement.ToString(); Console.WriteLine(elementInXml); } Console.WriteLine("This sample completed successfully; " + "press Enter to exit."); Console.ReadLine(); } // Add an attribute to the specified security element. private static SecurityElement AddAttribute( SecurityElement xmlElement, string attributeName, string attributeValue) { if (xmlElement != null) { // Verify that the attribute name and value are valid XML formats. if (SecurityElement.IsValidAttributeName(attributeName) && SecurityElement.IsValidAttributeValue(attributeValue)) { // Add the attribute to the security element. xmlElement.AddAttribute(attributeName, attributeValue); } } return xmlElement; } // Add a child element to the specified security element. private static SecurityElement AddChildElement( SecurityElement parentElement, string tagName, string tagText) { if (parentElement != null) { // Ensure that the tag text is in valid XML format. if (!SecurityElement.IsValidText(tagText)) { // Replace invalid text with valid XML text // to enforce proper XML formatting. tagText = SecurityElement.Escape(tagText); } // Determine whether the tag is in valid XML format. if (SecurityElement.IsValidTag(tagName)) { SecurityElement childElement; childElement = parentElement.SearchForChildByTag(tagName); if (childElement != null) { String elementText; elementText = parentElement.SearchForTextOfTag(tagName); if (!elementText.Equals(tagText)) { // Add child element to the parent security element. parentElement.AddChild( new SecurityElement(tagName, tagText)); } } else { // Add child element to the parent security element. parentElement.AddChild( new SecurityElement(tagName, tagText)); } } } return parentElement; } // Create and display a summary sentence // about the specified security element. private static void DisplaySummary(SecurityElement xmlElement) { // Retrieve tag name for the security element. string xmlTreeName = xmlElement.Tag.ToString(); // Retrieve tag text for the security element. string xmlTreeDescription = xmlElement.Text; // Retrieve value of the creationdate attribute. string xmlCreationDate = xmlElement.Attribute("creationdate"); // Retrieve the number of children under the security element. string childrenCount = xmlElement.Children.Count.ToString(); string outputMessage = "The security XML tree named " + xmlTreeName; outputMessage += "(" + xmlTreeDescription + ")"; outputMessage += " was created on " + xmlCreationDate + " and "; outputMessage += "contains " + childrenCount + " child elements."; Console.WriteLine(outputMessage); } // Compare the first two occurrences of an attribute // in the specified security element. private static void CompareAttributes( SecurityElement xmlElement, string attributeName) { // Create a hash table containing the security element's attributes. Hashtable attributeKeys = xmlElement.Attributes; string attributeValue = attributeKeys[attributeName].ToString(); foreach(SecurityElement xmlChild in xmlElement.Children) { if (attributeValue.Equals(xmlChild.Attribute(attributeName))) { // The security elements were created at the exact same time. } } } // Convert the contents of the specified security element // to hash codes stored in a hash table. private static void ConvertToHashTable(SecurityElement xmlElement) { // Create a hash table to hold hash codes of the security elements. Hashtable xmlAsHash = new Hashtable(); int rootIndex = xmlElement.GetHashCode(); xmlAsHash.Add(rootIndex, "root"); int parentNum = 0; foreach(SecurityElement xmlParent in xmlElement.Children) { parentNum++; xmlAsHash.Add(xmlParent.GetHashCode(), "parent" + parentNum); if ((xmlParent.Children != null) && (xmlParent.Children.Count > 0)) { int childNum = 0; foreach(SecurityElement xmlChild in xmlParent.Children) { childNum++; xmlAsHash.Add(xmlChild.GetHashCode(), "child" + childNum); } } } } // Delete the specified security element if the current time is past // the time stored in the destroytime tag. private static SecurityElement DestroyTree(SecurityElement xmlElement) { SecurityElement localXmlElement = xmlElement; SecurityElement destroyElement = localXmlElement.SearchForChildByTag("destroytime"); // Verify that a destroytime tag exists. if (localXmlElement.SearchForChildByTag("destroytime") != null) { // Retrieve the destroytime text to get the time // the tree can be destroyed. string storedDestroyTime = localXmlElement.SearchForTextOfTag("destroytime"); DateTime destroyTime = DateTime.Parse(storedDestroyTime); if (DateTime.Now > destroyTime) { localXmlElement = null; Console.WriteLine("The XML security tree has been deleted."); } } // Verify that xmlElement is of type SecurityElement. if (xmlElement.GetType().Equals( typeof(System.Security.SecurityElement))) { // Determine whether the localXmlElement object // differs from xmlElement. if (xmlElement.Equals(localXmlElement)) { // Verify that the tags, attributes and children of the // two security elements are identical. if (xmlElement.Equal(localXmlElement)) { // Return the original security element. return xmlElement; } } } // Return the modified security element. return localXmlElement; } } // // This sample produces the following output: // // The security XML tree named RootTag(XML security tree) // was created on 2/23/2004 1:23:00 PM and contains 2 child elements. //<RootTag creationdate="2/23/2004 1:23:00 PM">XML security tree // <destroytime>2/23/2004 1:23:01 PM</destroytime> // <WindowsMembership.WindowsRole version="1.00" // creationdate="2/23/2004 1:23:00 PM"> // <BabyElement>This is a child element.</BabyElement> // </WindowsMembership.WindowsRole> //</RootTag> // //This sample completed successfully; press Exit to continue.
using namespace System; using namespace System::Security; using namespace System::Collections; ref class SecurityElementMembers { public: [STAThread] int TestSecurityElementMembers() { SecurityElement^ xmlRootElement = gcnew SecurityElement( L"RootTag",L"XML security tree" ); AddAttribute( xmlRootElement, L"creationdate", DateTime::Now.ToString() ); AddChildElement( xmlRootElement, L"destroytime", DateTime::Now.AddSeconds( 1.0 ).ToString() ); SecurityElement^ windowsRoleElement = gcnew SecurityElement( L"WindowsMembership.WindowsRole" ); windowsRoleElement->AddAttribute( L"version", L"1.00" ); // Add a child element and a creationdate attribute. AddChildElement( windowsRoleElement, L"BabyElement", L"This is a child element" ); AddAttribute( windowsRoleElement, L"creationdate", DateTime::Now.ToString() ); xmlRootElement->AddChild( windowsRoleElement ); CompareAttributes( xmlRootElement, L"creationdate" ); ConvertToHashTable( xmlRootElement ); DisplaySummary( xmlRootElement ); // Determine if the security element is too old to keep. xmlRootElement = DestroyTree( xmlRootElement ); if ( xmlRootElement != nullptr ) { String^ elementInXml = xmlRootElement->ToString(); Console::WriteLine( elementInXml ); } Console::WriteLine( L"This sample completed successfully; " L"press Enter to exit." ); Console::ReadLine(); return 1; } private: // Add an attribute to the specified security element. static SecurityElement^ AddAttribute( SecurityElement^ xmlElement, String^ attributeName, String^ attributeValue ) { if ( xmlElement != nullptr ) { // Verify that the attribute name and value are valid XML formats. if ( SecurityElement::IsValidAttributeName( attributeName ) && SecurityElement::IsValidAttributeValue( attributeValue ) ) { // Add the attribute to the security element. xmlElement->AddAttribute( attributeName, attributeValue ); } } return xmlElement; } // Add a child element to the specified security element. static SecurityElement^ AddChildElement( SecurityElement^ parentElement, String^ tagName, String^ tagText ) { if ( parentElement != nullptr ) { // Ensure that the tag text is in valid XML format. if ( !SecurityElement::IsValidText( tagText ) ) { // Replace invalid text with valid XML text // to enforce proper XML formatting. tagText = SecurityElement::Escape( tagText ); } // Determine whether the tag is in valid XML format. if ( SecurityElement::IsValidTag( tagName ) ) { SecurityElement^ childElement; childElement = parentElement->SearchForChildByTag( tagName ); if ( childElement != nullptr ) { String^ elementText; elementText = parentElement->SearchForTextOfTag( tagName ); if ( !elementText->Equals( tagText ) ) { // Add child element to the parent security element. parentElement->AddChild( gcnew SecurityElement( tagName,tagText ) ); } } else { // Add child element to the parent security element. parentElement->AddChild( gcnew SecurityElement( tagName,tagText ) ); } } } return parentElement; } // Create and display a summary sentence // about the specified security element. static void DisplaySummary( SecurityElement^ xmlElement ) { // Retrieve tag name for the security element. String^ xmlTreeName = xmlElement->Tag->ToString(); // Retrieve tag text for the security element. String^ xmlTreeDescription = xmlElement->Text; // Retrieve value of the creationdate attribute. String^ xmlCreationDate = xmlElement->Attribute(L"creationdate"); // Retrieve the number of children under the security element. String^ childrenCount = xmlElement->Children->Count.ToString(); String^ outputMessage = String::Format( L"The security XML tree named {0}", xmlTreeName ); outputMessage = String::Concat( outputMessage, String::Format( L"({0})", xmlTreeDescription ) ); outputMessage = String::Concat( outputMessage, String::Format( L" was created on {0} and ", xmlCreationDate ) ); outputMessage = String::Concat( outputMessage, String::Format( L"contains {0} child elements.", childrenCount ) ); Console::WriteLine( outputMessage ); } // Compare the first two occurrences of an attribute // in the specified security element. static void CompareAttributes( SecurityElement^ xmlElement, String^ attributeName ) { // Create a hash table containing the security element's attributes. Hashtable^ attributeKeys = xmlElement->Attributes; String^ attributeValue = attributeKeys[ attributeName ]->ToString(); IEnumerator^ myEnum = xmlElement->Children->GetEnumerator(); while ( myEnum->MoveNext() ) { SecurityElement^ xmlChild = safe_cast<SecurityElement^>(myEnum->Current); if ( attributeValue->Equals( xmlChild->Attribute(attributeName) ) ) { // The security elements were created at the exact same time. } } } // Convert the contents of the specified security element // to hash codes stored in a hash table. static void ConvertToHashTable( SecurityElement^ xmlElement ) { // Create a hash table to hold hash codes of the security elements. Hashtable^ xmlAsHash = gcnew Hashtable; int rootIndex = xmlElement->GetHashCode(); xmlAsHash->Add( rootIndex, L"root" ); int parentNum = 0; IEnumerator^ myEnum1 = xmlElement->Children->GetEnumerator(); while ( myEnum1->MoveNext() ) { SecurityElement^ xmlParent = safe_cast<SecurityElement^>(myEnum1->Current); parentNum++; xmlAsHash->Add( xmlParent->GetHashCode(), String::Format( L"parent{0}", parentNum ) ); if ( (xmlParent->Children != nullptr) && (xmlParent->Children->Count > 0) ) { int childNum = 0; IEnumerator^ myEnum2 = xmlParent->Children->GetEnumerator(); while ( myEnum2->MoveNext() ) { SecurityElement^ xmlChild = safe_cast<SecurityElement^>(myEnum2->Current); childNum++; xmlAsHash->Add( xmlChild->GetHashCode(), String::Format( L"child{0}", childNum ) ); } } } } // Delete the specified security element if the current time is past // the time stored in the destroytime tag. static SecurityElement^ DestroyTree( SecurityElement^ xmlElement ) { SecurityElement^ localXmlElement = xmlElement; SecurityElement^ destroyElement = localXmlElement->SearchForChildByTag( L"destroytime" ); // Verify that a destroytime tag exists. if ( localXmlElement->SearchForChildByTag( L"destroytime" ) != nullptr ) { // Retrieve the destroytime text to get the time // the tree can be destroyed. String^ storedDestroyTime = localXmlElement->SearchForTextOfTag( L"destroytime" ); DateTime destroyTime = DateTime::Parse( storedDestroyTime ); if ( DateTime::Now > destroyTime ) { localXmlElement = nullptr; Console::WriteLine( L"The XML security tree has been deleted." ); } } // Verify that xmlElement is of type SecurityElement. if ( xmlElement->GetType()->Equals( System::Security::SecurityElement::typeid ) ) { // Determine whether the localXmlElement object // differs from xmlElement. if ( xmlElement->Equals( localXmlElement ) ) { // Verify that the tags, attributes and children of the // two security elements are identical. if ( xmlElement->Equal( localXmlElement ) ) { // Return the original security element. return xmlElement; } } } // Return the modified security element. return localXmlElement; } }; int main() { SecurityElementMembers^ sem = gcnew SecurityElementMembers; sem->TestSecurityElementMembers(); } // // This sample produces the following output: // // The security XML tree named RootTag(XML security tree) // was created on 2/23/2004 1:23:00 PM and contains 2 child elements. //<RootTag creationdate="2/23/2004 1:23:00 PM">XML security tree // <destroytime>2/23/2004 1:23:01 PM</destroytime> // <WindowsMembership.WindowsRole version="1.00" // creationdate="2/23/2004 1:23:00 PM"> // <BabyElement>This is a child element.</BabyElement> // </WindowsMembership.WindowsRole> //</RootTag> // //This sample completed successfully; press Exit to continue.
import System.*; import System.Security.*; import System.Collections.*; class SecurityElementMembers { /** @attribute STAThread() */ public static void main(String[] args) { SecurityElement xmlRootElement = new SecurityElement( "RootTag", "XML security tree"); AddAttribute(xmlRootElement, "creationdate", DateTime.get_Now().ToString()); AddChildElement(xmlRootElement, "destroytime", DateTime.get_Now().AddSeconds(1.0).ToString()); SecurityElement windowsRoleElement = new SecurityElement("WindowsMembership.WindowsRole"); windowsRoleElement.AddAttribute("version", "1.00"); // Add a child element and a creationdate attribute. AddChildElement(windowsRoleElement, "BabyElement", "This is a child element"); AddAttribute(windowsRoleElement, "creationdate", DateTime.get_Now().ToString()); xmlRootElement.AddChild(windowsRoleElement); CompareAttributes(xmlRootElement, "creationdate"); ConvertToHashTable(xmlRootElement); DisplaySummary(xmlRootElement); // Determine if the security element is too old to keep. xmlRootElement = DestroyTree(xmlRootElement); if (xmlRootElement != null) { String elementInXml = xmlRootElement.ToString(); Console.WriteLine(elementInXml); } Console.WriteLine("This sample completed successfully; " + "press Enter to exit."); Console.ReadLine(); } //main // Add an attribute to the specified security element. private static SecurityElement AddAttribute(SecurityElement xmlElement, String attributeName, String attributeValue) { if (xmlElement != null) { // Verify that the attribute name and value are valid XML formats. if (SecurityElement.IsValidAttributeName(attributeName) && SecurityElement.IsValidAttributeValue(attributeValue)) { // Add the attribute to the security element. xmlElement.AddAttribute(attributeName, attributeValue); } } return xmlElement; } //AddAttribute // Add a child element to the specified security element. private static SecurityElement AddChildElement( SecurityElement parentElement, String tagName, String tagText) { if (parentElement != null) { // Ensure that the tag text is in valid XML format. if (!(SecurityElement.IsValidText(tagText))) { // Replace invalid text with valid XML text // to enforce proper XML formatting. tagText = SecurityElement.Escape(tagText); } // Determine whether the tag is in valid XML format. if (SecurityElement.IsValidTag(tagName)) { SecurityElement childElement; childElement = parentElement.SearchForChildByTag(tagName); if (childElement != null) { String elementText; elementText = parentElement.SearchForTextOfTag(tagName); if (!(elementText.Equals(tagText))) { // Add child element to the parent security element. parentElement.AddChild(new SecurityElement(tagName , tagText)); } } else { // Add child element to the parent security element. parentElement.AddChild(new SecurityElement(tagName , tagText)); } } } return parentElement; } //AddChildElement // Create and display a summary sentence // about the specified security element. private static void DisplaySummary(SecurityElement xmlElement) { // Retrieve tag name for the security element. String xmlTreeName = xmlElement.get_Tag().ToString(); // Retrieve tag text for the security element. String xmlTreeDescription = xmlElement.get_Text(); // Retrieve value of the creationdate attribute. String xmlCreationDate = xmlElement.Attribute("creationdate"); // Retrieve the number of children under the security element. String childrenCount = System.Convert.ToString(xmlElement.get_Children().get_Count()); String outputMessage = "The security XML tree named " + xmlTreeName; outputMessage += "(" + xmlTreeDescription + ")"; outputMessage += " was created on " + xmlCreationDate + " and "; outputMessage += "contains " + childrenCount + " child elements."; Console.WriteLine(outputMessage); } //DisplaySummary // Compare the first two occurrences of an attribute // in the specified security element. private static void CompareAttributes(SecurityElement xmlElement, String attributeName) { // Create a hash table containing the security element's attributes. Hashtable attributeKeys = xmlElement.get_Attributes(); String attributeValue = attributeKeys.get_Item(attributeName).ToString(); for (int iCtr = 0; iCtr < xmlElement.get_Children().get_Count(); iCtr++) { SecurityElement xmlChild = (SecurityElement)xmlElement.get_Children().get_Item(iCtr); if (attributeValue.Equals(xmlChild.Attribute(attributeName))) { } } } //CompareAttributes // The security elements were created at the exact same time. // Convert the contents of the specified security element // to hash codes stored in a hash table. private static void ConvertToHashTable(SecurityElement xmlElement) { // Create a hash table to hold hash codes of the security elements. Hashtable xmlAsHash = new Hashtable(); int rootIndex = xmlElement.GetHashCode(); xmlAsHash.Add((Int32)rootIndex, "root"); int parentNum = 0; for (int iCtr1 = 0; iCtr1 < xmlElement.get_Children().get_Count(); iCtr1++) { SecurityElement xmlParent = (SecurityElement)xmlElement.get_Children().get_Item(iCtr1); parentNum++; xmlAsHash.Add((Int32)xmlParent.GetHashCode(), "parent" + parentNum); if (xmlParent.get_Children() != null && xmlParent.get_Children().get_Count() > 0) { int childNum = 0; for (int iCtr2 = 0; iCtr2 < xmlParent.get_Children().get_Count(); iCtr2++) { SecurityElement xmlChild = (SecurityElement)xmlParent. get_Children().get_Item(iCtr2); childNum++; xmlAsHash.Add((Int32)xmlChild.GetHashCode(), "child" + childNum); } } } } //ConvertToHashTable // Delete the specified security element if the current time is past // the time stored in the destroytime tag. private static SecurityElement DestroyTree(SecurityElement xmlElement) { SecurityElement localXmlElement = xmlElement; SecurityElement destroyElement = localXmlElement.SearchForChildByTag("destroytime"); // Verify that a destroytime tag exists. if (localXmlElement.SearchForChildByTag("destroytime") != null) { // Retrieve the destroytime text to get the time // the tree can be destroyed. String storedDestroyTime = localXmlElement.SearchForTextOfTag( "destroytime"); DateTime destroyTime = DateTime.Parse(storedDestroyTime); if ((DateTime.get_Now().CompareTo(destroyTime)) > 0) { localXmlElement = null; Console.WriteLine("The XML security tree has been deleted."); } } // Verify that xmlElement is of type SecurityElement. if (xmlElement.GetType().Equals( System.Security.SecurityElement.class.ToType())) { // Determine whether the localXmlElement object // differs from xmlElement. if (xmlElement.Equals(localXmlElement)) { // Verify that the tags, attributes and children of the // two security elements are identical. if (xmlElement.Equal(localXmlElement)) { // Return the original security element. return xmlElement; } } } // Return the modified security element. return localXmlElement; } //DestroyTree } //SecurityElementMembers // // This sample produces the following output: // // The security XML tree named RootTag(XML security tree) // was created on 2/23/2004 1:23:00 PM and contains 2 child elements. //<RootTag creationdate="2/23/2004 1:23:00 PM">XML security tree // <destroytime>2/23/2004 1:23:01 PM</destroytime> // <WindowsMembership.WindowsRole version="1.00" // creationdate="2/23/2004 1:23:00 PM"> // <BabyElement>This is a child element.</BabyElement> // </WindowsMembership.WindowsRole> //</RootTag> // //This sample completed successfully; press Exit to continue.

System.Security.SecurityElement


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SecurityElement クラスのページへのリンク