本文整理汇总了C#中System.Xml.Serialization.XmlSerializer.CanDeserialize方法的典型用法代码示例。如果您正苦于以下问题:C# XmlSerializer.CanDeserialize方法的具体用法?C# XmlSerializer.CanDeserialize怎么用?C# XmlSerializer.CanDeserialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.XmlSerializer
的用法示例。
在下文中一共展示了XmlSerializer.CanDeserialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddFile
public void AddFile(string fileName)
{
if (!File.Exists(fileName))
{
throw new DeviceDetectorException("Specified browser file doesn't exist", new FileNotFoundException(string.Empty, fileName));
}
if (String.Compare(Path.GetExtension(fileName), ".browser", true) != 0)
{
throw new DeviceDetectorException("Specified file has invalid extension");
}
using (XmlReader xmlReader = XmlReader.Create(fileName))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(browsers));
if (!xmlSerializer.CanDeserialize(xmlReader))
{
throw new DeviceDetectorException("Invalid file format");
}
try
{
browsers browsersFile = xmlSerializer.Deserialize(xmlReader) as browsers;
string keyName = Path.GetFileNameWithoutExtension(fileName);
if (!this.browserFiles.ContainsKey(keyName))
{
this.browserFiles.Add(keyName, browsersFile);
}
}
catch (InvalidOperationException ex)
{
throw new DeviceDetectorException("Cannot parse specified file \"" + fileName + "\"", ex);
}
}
}
示例2: LoadFromAppDataAsync
public static async Task<Rules> LoadFromAppDataAsync () {
var d20Rules = new D20Rules() { Rules = new Rules() };
var appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var basePath = Path.Combine(appPath, "CBLoader");
var rulesPath = Path.Combine(basePath, "combined.dnd40");
if (!File.Exists(rulesPath)) {
return d20Rules.Rules;
}
Stopwatch timer = Stopwatch.StartNew();
try {
d20Rules = await Task.Run<D20Rules>(() => {
XmlSerializer serializer = new XmlSerializer(typeof(D20Rules));
using (var file = new FileStream(rulesPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, useAsync: true)) {
using (var reader = new XmlTextReader(file)) {
if (serializer.CanDeserialize(reader)) {
return serializer.Deserialize(reader) as D20Rules;
}
}
}
return d20Rules;
}).ConfigureAwait(false);
}
finally {
timer.Stop();
Trace.TraceInformation("Deserializing D20Rules took {0}ms", timer.ElapsedMilliseconds);
}
return d20Rules.Rules;
}
示例3: Consult
public static ConsultResponse Consult(this GlobalMedicalFileConsultationPortTypeClient client, CommonInputType common, RoutingType routing, RetrieveTransactionRequestType detailValue, out ArchivingInfo archivingInfo)
{
var detail = new BlobType();
detail.Id = "_" + Guid.NewGuid().ToString();
detail.ContentType = "text/xml";
detail.ContentEncoding = "none";
var detailStream = new MemoryStream();
var serializer = new XmlSerializer(typeof(RetrieveTransactionRequestType));
serializer.Serialize(detailStream, detailValue);
detail.Value = detailStream.ToArray();
ResponseReturnType super = client.Consult(common, routing, detail);
archivingInfo = new ArchivingInfo();
archivingInfo.RequestDetail = detail;
archivingInfo.RequestXadesT = null;
archivingInfo.ResponseDetail = super.Detail;
archivingInfo.ResponseXadesT = super.XadesT;
var retVal = new ConsultResponse();
retVal.Common = super.CommonOutput;
if (super.Detail.ContentType == "text/xml"
&& super.Detail.ContentEncoding == "none")
{
var reader = XmlReader.Create(new MemoryStream(super.Detail.Value));
var deserializer = new XmlSerializer(typeof(RetrieveTransactionResponseType));
if (deserializer.CanDeserialize(reader))
{
retVal.DetailValue = deserializer.Deserialize(reader) as RetrieveTransactionResponseType;
}
}
return retVal;
}
示例4: Load
public Config Load()
{
if (File.Exists(CONFIG_FILE))
{
StreamReader sr = new StreamReader(CONFIG_FILE);
XmlTextReader xr = new XmlTextReader(sr);
XmlSerializer xs = new XmlSerializer(this.GetType());
object c;
if (xs.CanDeserialize(xr))
{
c = xs.Deserialize(xr);
Type t = this.GetType();
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
p.SetValue(this, p.GetValue(c, null), null);
}
}
xr.Close();
sr.Close();
}
else
{
Logger.Warn("Configuration file not found - loading defaults.");
Save();
}
return this;
}
示例5: Load
public static PacketsHolder Load()
{
PacketsHolder opts = new PacketsHolder();
try
{
if (File.Exists(PacketsHolder.FileName))
{
using (FileStream fs = new FileStream(PacketsHolder.FileName, FileMode.Open))
{
using (XmlReader xr = new XmlTextReader(fs))
{
XmlSerializer ser = new XmlSerializer(typeof(PacketsHolder));
if (ser.CanDeserialize(xr))
{
opts = (PacketsHolder)ser.Deserialize(xr);
}
}
}
}
}
catch (InvalidOperationException)
{
}
catch (XmlException)
{
}
if (opts == null)
opts = new PacketsHolder();
return opts;
}
示例6: CanDeserializeRssFeedFromXml
public void CanDeserializeRssFeedFromXml()
{
var testfeed = TestFileLoader.GetSingleRssTestFilePath(TestFileLoader.TestFileType.FileSys);
var feed = Factory.CreateFeed(new Uri(testfeed));
var serializer = new XmlSerializer(feed.GetType());
using (var filestream = new FileStream(RssXmlFile, FileMode.Create))
{
serializer.Serialize(filestream, feed);
}
using (var readstream = new FileStream(RssXmlFile, FileMode.Open))
{
using(var reader = XmlReader.Create(readstream))
{
Assert.IsTrue(serializer.CanDeserialize(reader));
var resultantObject = serializer.Deserialize(reader);
//Assert that the result object is of the expected type (some derivative of IFeed)
Assert.IsInstanceOf(feed.GetType(), resultantObject);
//Assert that the two objects are distinct instances
Assert.AreNotSame(feed, resultantObject);
//Cast the object back into an IFeed and perform some specific assertions
var resultantFeed = resultantObject as IFeed;
Assert.AreEqual(feed.Title, resultantFeed.Title);
Assert.AreEqual(feed.LastUpdated, resultantFeed.LastUpdated);
Assert.AreEqual(feed.FeedUri, resultantFeed.FeedUri);
Assert.AreEqual(feed.FeedType, resultantFeed.FeedType);
Assert.AreEqual(feed.Items.Count, resultantFeed.Items.Count);
Assert.Pass("THE FEED CAN BE DESERIALIZED SUCCESSFULLY");
}
}
}
示例7: CommandTest
public void CommandTest()
{
XmlSerializer serializer = new XmlSerializer(typeof(Command));
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlWriter.Create(stringWriter);
Command cmd = new Command(); // TODO: Initialize to an appropriate value
cmd.Method = "RunLine";//RunLine(float length, float speed, float acceleration)
cmd.Parameters.Add(1.0f);
cmd.Parameters.Add(1.0f);
cmd.Parameters.Add(1.0f);
serializer.Serialize(xmlWriter, cmd);
xmlWriter.Flush();
Debug.WriteLine(stringWriter.ToString());
//XmlSerializer serializer = new XmlSerializer(typeof(Command));
StringReader stringReader = new StringReader(stringWriter.ToString());
XmlReader xmlReader = XmlReader.Create(stringReader);
if (serializer.CanDeserialize(xmlReader))
{
object o = serializer.Deserialize(xmlReader);
if (o is Command)
{
Command cmdDeserialized = (Command)o;
Debug.WriteLine(cmdDeserialized.ToString());
}
}
}
示例8: ReadShortcut
/// <summary>
/// Returns the shortcut from the specified file.
/// </summary>
/// <exception cref="FileNotFoundException">The file not found.</exception>
/// <exception cref="ShortcutFileAccessException">The format of the shortcut was invalid</exception>
public IShortcut ReadShortcut()
{
if (!File.Exists(this.FilePath))
{
throw new FileNotFoundException();
}
try
{
// Load the file
var xDoc = XDocument.Load(this.FilePath);
var serializer = new XmlSerializer(typeof(Shortcut));
var reader = xDoc.CreateReader();
if (!serializer.CanDeserialize(reader))
{
throw new ShortcutFileFormatException();
}
// Deserialize the shortcut
var shortcut = (Shortcut)serializer.Deserialize(reader);
return shortcut;
}
catch (Exception)
{
throw new ShortcutFileFormatException();
}
}
示例9: GetReply
public static Q3ApiReply GetReply(XmlReader r)
{
XmlSerializer s = new XmlSerializer(typeof(Q3ApiReply));
if (s.CanDeserialize(r))
return s.Deserialize(r) as Q3ApiReply;
else
return null;
}
示例10: GetXmlData
public static Package GetXmlData(string path)
{
var xml = XDocument.Parse(File.ReadAllText(path));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Package));
if (xmlSerializer.CanDeserialize(xml.CreateReader()))
{
Package serXml = (Package)xmlSerializer.Deserialize(xml.CreateReader());
return serXml;
}
return null;
}
示例11: GetRedirectList
private static AssemblyBinding GetRedirectList(XElement element)
{
var serializer = new XmlSerializer(typeof(AssemblyBinding));
if (!serializer.CanDeserialize(element.CreateReader()))
{
Error("Unable to deserialize assembly bindings, please ensure xml is valid", true);
return null;
}
return serializer.Deserialize(element.CreateReader()) as AssemblyBinding;
}
示例12: DeserializeXml
/// <summary>
/// Deserialize an object from UTF8 Xml
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object DeserializeXml(Type type, string xml)
{
object data = null;
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)))
{
XmlSerializer xs = new XmlSerializer(type);
XmlReader xr = XmlReader.Create(ms);
if (xs.CanDeserialize(xr))
data = xs.Deserialize(xr);
}
return data;
}
示例13: Will_generate_trx_xml
public void Will_generate_trx_xml()
{
//Since the generated file is through GUID, we cannot do a simply string compare.
var transformer = new TrxXmlTransformer(GetFileSystemWrapper());
var summary = BuildTestCaseSummary();
var result = transformer.Transform(summary);
XmlReader xr = new XmlTextReader(new StringReader(result));
XmlSerializer xs = new XmlSerializer(typeof(TestRunType));
Assert.True(xs.CanDeserialize(xr));
TestRunType trx = (TestRunType)xs.Deserialize(xr);
var testDefinitions =
trx.Items.GetInstance<TestDefinitionType>(VSTSExtensions.TestRunItemType.TestDefinition).Items.Cast<UnitTestType>().ToArray();
Assert.Equal(testDefinitions.Count(), 4);
for (int i = 0; i < testDefinitions.Count(); i++)
{
var vststUnitTest = testDefinitions[i];
var testSummary = summary.Tests[i];
Assert.Equal(vststUnitTest.TestMethod.name, testSummary.TestName);
Assert.Equal(vststUnitTest.TestMethod.adapterTypeName, "Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter");
}
var testResults =
trx.Items.GetInstance<ResultsType>(VSTSExtensions.TestRunItemType.Results).Items.Cast<UnitTestResultType>().ToArray();
Assert.Equal(testResults.Count(), 4);
for (int i = 0; i < testResults.Count(); i++)
{
var vststUnitTestResult = testResults[i];
var testSummary = summary.Tests[i];
Assert.Equal(vststUnitTestResult.testName,testSummary.TestName);
Assert.Equal(vststUnitTestResult.outcome,testSummary.ResultsAllPassed ? "Passed":"Failed");
if (vststUnitTestResult.Items != null && vststUnitTestResult.Items.Any())
Assert.Equal(((OutputType)vststUnitTestResult.Items[0]).ErrorInfo.Message, testSummary.TestResults[0].Message);
}
var counters = (CountersType)
trx.Items.GetInstance<TestRunTypeResultSummary>(VSTSExtensions.TestRunItemType.ResultSummary)
.Items.First();
Assert.Equal(counters.passed,2);
Assert.Equal(counters.failed,2);
}
示例14: XmlImportTest1
public void XmlImportTest1()
{
var actual = new FeedConfigItemList();
var serializer = new XmlSerializer(actual.GetType());
var sr = new StringReader(Properties.Resources.XmlImportTest1);
var xmlr = XmlReader.Create(sr);
if (serializer.CanDeserialize(xmlr))
{
actual = (FeedConfigItemList)serializer.Deserialize(xmlr);
}
xmlr.Close();
sr.Close();
Assert.AreEqual(16, actual.Items.Count);
}
示例15: Deserialise
public MessageWrapper Deserialise(string xml)
{
var x = new XmlSerializer(typeof(MessageWrapper));
if (x.CanDeserialize(XmlReader.Create(new StringReader(xml))))
{
var message = x.Deserialize(new StringReader(xml));
return message as MessageWrapper;
}
else
{
return null;
}
}