有没有办法将额外的XML标签捕获到您未在课堂上预期的文件中? 例如:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace XmlDeserializerTest
{
class Program
{
static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(PersonInfo));
StreamReader reader = new StreamReader(@"C:\XML\Xml.xml");
object obj = deserializer.Deserialize(reader);
PersonInfo D = (PersonInfo) obj;
Console.WriteLine(D.address.Age);
reader.Close();
Console.ReadLine();
}
}
[XmlRoot("MyInfo")]
public class PersonInfo
{
public string Name { get; set; }
public string Type { get; set; }
[XmlElement("Address")]
public Loc address = new Loc();
}
public class Loc
{
public string Age { get; set; }
public string Location { get; set; }
}
// File used by this program:
// <?xml version="1.0" encoding="utf-8"?>
// <MyInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <Address>
// <Age>51</Age>
// <Location>Tulsa</Location>
// <State>Oklahoma</State>
// </Address>
// <Name>Scott</Name>
// <Type>Programmer</Type>
//</MyInfo>
}
这不会产生错误,它只是不加载状态信息。它只是忽略它。我想知道是否有办法陷阱或将额外的代码发送到另一个类或其他什么。
谢谢, 斯科特