XmlAnyAttributeAttribute Costruttore
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Consente di creare una nuova istanza della classe XmlAnyAttributeAttribute.
public:
XmlAnyAttributeAttribute();
public XmlAnyAttributeAttribute ();
Public Sub New ()
Esempio
Nell'esempio seguente viene costruito un oggetto XmlAnyAttributeAttribute utilizzato per eseguire l'override della deserializzazione di un oggetto. Per provare l'esempio, creare un file denominato UnknownAttributes.xml contenente il codice XML seguente:
<?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