using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.IO;
using System.Xml.XmlConfiguration;
using OATAssetTracking.Solution.Presenter;
namespace WindowsFormsApplication2
{
public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
string path = "SearchDefinition.xml";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, @"D:\searchDefinition.xsd");
Exception firstException = null;
var settings = new XmlReaderSettings
{
Schemas = schemas,
ValidationType = ValidationType.Schema,
ValidationFlags =
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ReportValidationWarnings
};
settings.ValidationEventHandler +=
delegate(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
{
Console.WriteLine(args.Message);
}
else
{
if (firstException == null)
{
firstException = args.Exception;
}
Console.WriteLine(args.Exception.ToString());
}
};
Search result=null;
using (var input = new StreamReader(path))
{
using (XmlReader reader = XmlReader.Create(input, settings))
{
XmlSerializer ser = new XmlSerializer(typeof(Search));
result = (Search)ser.Deserialize(reader);
}
}
if (firstException != null)
{
throw firstException;
}
MessageBox.Show("Deserialization Done!!!!");
}
}
}Deserialize XML in C# Windows-Geräte-Projekt zum Objekt
In diesem,
Hier suchen ist Klassenname, der von XSD generiert
result = (Search) ser. Deserialisieren (Leser);
Diese obige Anweisung funktioniert gut in Windows Forms Application, aber funktioniert nicht mit Windows Device Project "Keine Fehler, aber hängt nur dort nicht ausgeführt werden".
Erhm ... nur neugierig, was ist ein "Windows Device" -Projekt? Ich habe VS2015 pro und kann diesen Projekttyp nicht finden ... vielleicht meinst du UWP? – Gusman
Das Projekt ist Smart Device Project, das unter .NET Framework 3.5 in VS2008 kommt –