IXmlSerializable.ReadXml(XmlReader) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Generates an object from its XML representation.
public:
void ReadXml(System::Xml::XmlReader ^ reader);
public void ReadXml (System.Xml.XmlReader reader);
abstract member ReadXml : System.Xml.XmlReader -> unit
Public Sub ReadXml (reader As XmlReader)
Parameters
Examples
The following example illustrates an implementation of the ReadXml method.
virtual void ReadXml( XmlReader^ reader )
{
personName = reader->ReadString();
}
public void ReadXml (XmlReader reader)
{
personName = reader.ReadString();
}
The following example illustrates the use of the XmlSerializer class to deserialize this object.
#using <System.Xml.dll>
#using <System.dll>
#using <Person.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
int main()
{
XmlSerializer^ serializer = gcnew XmlSerializer( Person::typeid );
FileStream^ file = gcnew FileStream( "test.xml",FileMode::Open );
Person^ aPerson = dynamic_cast<Person^>(serializer->Deserialize( file ));
Console::WriteLine( aPerson );
}
using System;
using System.IO;
using System.Xml.Serialization;
public class Reader {
public static void Main() {
XmlSerializer serializer = new XmlSerializer(typeof(Person));
FileStream file = new FileStream("test.xml", FileMode.Open);
Person aPerson = (Person) serializer.Deserialize(file);
Console.WriteLine(aPerson);
}
}
Remarks
The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.
When this method is called, the reader is positioned on the start tag that wraps the information for your type. That is, directly on the start tag that indicates the beginning of a serialized object. When this method returns, it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.
When implementing this method, you should consider the possibility that a malicious user might provide a well-formed but invalid XML representation in order to disable or otherwise alter the behavior of your application.
Applies to
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기