XmlAttributes.XmlRoot 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XmlSerializer가 클래스를 XML 루트 요소로 serialize하는 방식을 지정하는 개체를 가져오거나 설정합니다.
public:
property System::Xml::Serialization::XmlRootAttribute ^ XmlRoot { System::Xml::Serialization::XmlRootAttribute ^ get(); void set(System::Xml::Serialization::XmlRootAttribute ^ value); };
public System.Xml.Serialization.XmlRootAttribute XmlRoot { get; set; }
public System.Xml.Serialization.XmlRootAttribute? XmlRoot { get; set; }
member this.XmlRoot : System.Xml.Serialization.XmlRootAttribute with get, set
Public Property XmlRoot As XmlRootAttribute
속성 값
XML 루트 요소로 지정된 클래스를 재정의하는 XmlRootAttribute입니다.
예제
다음 예제에서는 XmlAttributeOverrides 개체를 XmlAttributes 개체 및 XmlRootAttribute 개체입니다. 예제는 XmlRootAttribute 에 XmlRoot 의 속성을 XmlAttributes 개체를 추가 XmlAttributes 개체를 XmlAttributeOverrides 개체입니다. 마지막으로,이 예제에서는 가져옵니다는 XmlAttributes 전달 하 여 개체를 Type serialize 된 클래스의는 XmlAttributeOverrides 개체. (이 예는 Type 는 Group
.)
#using <System.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
// This is the class that will be serialized.
public ref class Group
{
public:
String^ GroupName;
[XmlAttributeAttribute]
int GroupCode;
};
// Return an XmlSerializer for overriding attributes.
XmlSerializer^ CreateOverrider()
{
// Create the XmlAttributes and XmlAttributeOverrides objects.
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
XmlRootAttribute^ xRoot = gcnew XmlRootAttribute;
// Set a new Namespace and ElementName for the root element.
xRoot->Namespace = "http://www.cpandl.com";
xRoot->ElementName = "NewGroup";
attrs->XmlRoot = xRoot;
/* Add the XmlAttributes object to the XmlAttributeOverrides.
No member name is needed because the whole class is
overridden. */
xOver->Add( Group::typeid, attrs );
// Get the XmlAttributes object, based on the type.
XmlAttributes^ tempAttrs;
tempAttrs = xOver[ Group::typeid ];
// Print the Namespace and ElementName of the root.
Console::WriteLine( tempAttrs->XmlRoot->Namespace );
Console::WriteLine( tempAttrs->XmlRoot->ElementName );
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
return xSer;
}
void SerializeObject( String^ filename )
{
// Create the XmlSerializer using the CreateOverrider method.
XmlSerializer^ xSer = CreateOverrider();
// Create the object to serialize.
Group^ myGroup = gcnew Group;
myGroup->GroupName = ".NET";
myGroup->GroupCode = 123;
// To write the file, a TextWriter is required.
TextWriter^ writer = gcnew StreamWriter( filename );
// Serialize the object and close the TextWriter.
xSer->Serialize( writer, myGroup );
writer->Close();
}
int main()
{
SerializeObject( "OverrideRoot.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
public string GroupName;
[XmlAttribute]
public int GroupCode;
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.SerializeObject("OverrideRoot.xml");
}
// Return an XmlSerializer for overriding attributes.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributes and XmlAttributeOverrides objects.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlRootAttribute xRoot = new XmlRootAttribute();
// Set a new Namespace and ElementName for the root element.
xRoot.Namespace = "http://www.cpandl.com";
xRoot.ElementName = "NewGroup";
attrs.XmlRoot = xRoot;
/* Add the XmlAttributes object to the XmlAttributeOverrides.
No member name is needed because the whole class is
overridden. */
xOver.Add(typeof(Group), attrs);
// Get the XmlAttributes object, based on the type.
XmlAttributes tempAttrs;
tempAttrs = xOver[typeof(Group)];
// Print the Namespace and ElementName of the root.
Console.WriteLine(tempAttrs.XmlRoot.Namespace);
Console.WriteLine(tempAttrs.XmlRoot.ElementName);
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
public void SerializeObject(string filename)
{
// Create the XmlSerializer using the CreateOverrider method.
XmlSerializer xSer = CreateOverrider();
// Create the object to serialize.
Group myGroup = new Group();
myGroup.GroupName = ".NET";
myGroup.GroupCode = 123;
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(filename);
// Serialize the object and close the TextWriter.
xSer.Serialize(writer, myGroup);
writer.Close();
}
}
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Group
Public GroupName As String
<XmlAttribute()> Public GroupCode As Integer
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializeObject("OverrideRoot.xml")
End Sub
' Return an XmlSerializer for overriding attributes.
Public Function CreateOverrider() As XmlSerializer
' Create the XmlAttributes and XmlAttributeOverrides objects.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
Dim xRoot As New XmlRootAttribute()
' Set a new Namespace and ElementName for the root element.
xRoot.Namespace = "http://www.cpandl.com"
xRoot.ElementName = "NewGroup"
attrs.XmlRoot = xRoot
' Add the XmlAttributes object to the XmlAttributeOverrides.
' No member name is needed because the whole class is
' overridden.
xOver.Add(GetType(Group), attrs)
' Get the XmlAttributes object, based on the type.
Dim tempAttrs As XmlAttributes
tempAttrs = xOver(GetType(Group))
' Print the Namespace and ElementName of the root.
Console.WriteLine(tempAttrs.XmlRoot.Namespace)
Console.WriteLine(tempAttrs.XmlRoot.ElementName)
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create the XmlSerializer using the CreateOverrider method.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object to serialize.
Dim myGroup As New Group()
myGroup.GroupName = ".NET"
myGroup.GroupCode = 123
' To write the file, a TextWriter is required.
Dim writer As New StreamWriter(filename)
' Serialize the object and close the TextWriter.
xSer.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class