XmlAnyAttributeAttribute 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XmlAnyAttributeAttribute 클래스의 새 인스턴스를 만듭니다.
public:
XmlAnyAttributeAttribute();
public XmlAnyAttributeAttribute ();
Public Sub New ()
예제
다음 예제에서는 개체의 역직렬화를 재정의하는 데 사용되는 구문을 생성 XmlAnyAttributeAttribute 합니다. 이 예제를 시도하려면 다음 XML이 포함된 UnknownAttributes.xml 파일을 만듭니다.
<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>
<GroupName>MyGroup</GroupName>
</Group>
#using <System.dll>
#using <System.XML.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
public ref class Group
{
public:
String^ GroupName;
// The Things array will be used to collect all unknown
// attributes found when deserializing.
array<XmlAttribute^>^Things;
};
XmlSerializer^ CreateOverrideSerializer();
void DeserializeObject( String^ filename )
{
// Use the CreateOverrideSerializer to return an instance
// of the XmlSerializer customized for overrides.
XmlSerializer^ ser = CreateOverrideSerializer();
// A FileStream is needed to read the XML document.
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Group^ g = safe_cast<Group^>(ser->Deserialize( fs ));
fs->Close();
Console::WriteLine( g->GroupName );
Console::WriteLine( g->Things->Length );
for ( IEnumerator ^ e = g->Things->GetEnumerator(); e->MoveNext(); )
{
XmlAttribute^ xAtt = safe_cast<XmlAttribute^>(e->Current);
Console::WriteLine( "{0}: {1}", xAtt->Name, xAtt->InnerXml );
}
}
XmlSerializer^ CreateOverrideSerializer()
{
// Override the Things field to capture all
// unknown XML attributes.
XmlAnyAttributeAttribute^ myAnyAttribute = gcnew XmlAnyAttributeAttribute;
XmlAttributeOverrides^ xOverride = gcnew XmlAttributeOverrides;
XmlAttributes^ xAtts = gcnew XmlAttributes;
xAtts->XmlAnyAttribute = myAnyAttribute;
xOverride->Add( Group::typeid, "Things", xAtts );
return gcnew XmlSerializer( Group::typeid,xOverride );
}
int main()
{
DeserializeObject( "UnknownAttributes.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
public class Group{
public string GroupName;
// The Things array will be used to collect all unknown
// attributes found when deserializing.
public XmlAttribute[]Things;
}
public class Test{
static void Main(){
Test t = new Test();
t.DeserializeObject("UnknownAttributes.xml");
}
private void DeserializeObject(string filename){
// Use the CreateOverrideSerializer to return an instance
// of the XmlSerializer customized for overrides.
XmlSerializer ser = CreateOverrideSerializer();
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
Group g = (Group)
ser.Deserialize(fs);
fs.Close();
Console.WriteLine(g.GroupName);
Console.WriteLine(g.Things.Length);
foreach(XmlAttribute xAtt in g.Things){
Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
}
}
private XmlSerializer CreateOverrideSerializer(){
// Override the Things field to capture all
// unknown XML attributes.
XmlAnyAttributeAttribute myAnyAttribute =
new XmlAnyAttributeAttribute();
XmlAttributeOverrides xOverride =
new XmlAttributeOverrides();
XmlAttributes xAtts = new XmlAttributes();
xAtts.XmlAnyAttribute=myAnyAttribute;
xOverride.Add(typeof(Group), "Things", xAtts);
return new XmlSerializer(typeof(Group) , xOverride);
}
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Public Class Group
' The Things array will be used to collect all unknown
' attributes found when deserializing.
public GroupName As String
public Things() As XmlAttribute
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.DeserializeObject("UnknownAttributes.xml")
End Sub
Private Sub SerializeObject(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType (Group))
Dim writer As TextWriter = New StreamWriter(filename)
Dim g As Group = New Group()
g.GroupName = "MyGroup"
ser.Serialize(writer, g)
writer.Close()
End Sub
Private Sub DeserializeObject(filename As String)
' Use the CreateOverrideSerializer to return an instance
' of the XmlSerializer customized for overrides.
Dim ser As XmlSerializer = CreateOverrideSerializer()
' A FileStream is needed to read the XML document.
Dim fs As FileStream = New FileStream(filename, FileMode.Open)
Dim g As Group = CType( _
ser.Deserialize(fs), Group)
fs.Close()
Console.WriteLine(g.GroupName)
Console.WriteLine(g.Things.Length)
Dim xAtt As XmlAttribute
for each xAtt in g.Things
Console.WriteLine(xAtt.Name & ": " & xAtt.InnerXml)
Next
End Sub
Private Function CreateOverrideSerializer() As XmlSerializer
' Override the Things field to capture all
' unknown XML attributes.
Dim myAnyAttribute As XmlAnyAttributeAttribute = _
New XmlAnyAttributeAttribute()
Dim xOverride As XmlAttributeOverrides = _
New XmlAttributeOverrides()
Dim xAtts As XmlAttributes = New XmlAttributes()
xAtts.XmlAnyAttribute=myAnyAttribute
xOverride.Add(GetType(Group), "Things", xAtts)
return New XmlSerializer(GetType(Group) , xOverride)
End Function
End Class