【发布时间】:2014-04-15 00:50:25
【问题描述】:
我想导入一个 xml 文件并将其反序列化到我的模型对象中。
我正在使用 C# MVC4 Asp.Net 4.51
我已经尝试了多种方法 - 如果有人可以就实现这一目标的最佳方式提供一些指导,我将不胜感激。
我已经尝试过,但无法让它工作:
public static KronosPunchRoundRuleSummary Deserialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(KronosPunchRoundRuleSummary));
TextReader textReader;
textReader = new StreamReader(@"c:\WSAPunchRoundRule.xml");
KronosPunchRoundRuleSummary summary = (KronosPunchRoundRuleSummary)serializer.Deserialize(textReader);
textReader.Close();
}
我也尝试过 thisenter code here,但我收到一条错误消息,说它不需要 Kronos_WFC xmlns:
public static KronosPunchRoundRuleSummary Deserialize()
{
//XmlSerializer serializer = new XmlSerializer(typeof(KronosPunchRoundRuleSummary));
//TextReader textReader;
//textReader = new StreamReader(@"c:\WSAPunchRoundRule.xml");
//KronosPunchRoundRuleSummary summary = (KronosPunchRoundRuleSummary)serializer.Deserialize(textReader);
//textReader.Close();
XmlDocument doc = new XmlDocument();
doc.Load(@"c:\WSAPunchRoundRule.xml");
string xmlcontents = doc.InnerXml;
return null;
}
xml文件是这样的
`<?xml version="1.0"?>
-<Kronos_WFC TimeStamp="03/12/2013 14:59 GMT+04:00" WFCVersion="6.2.11.231" VERSION="1.0">
-<Response action="RetrieveAllForUpdate" Status="Success">
<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Default Early Start" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>
<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Ramadan" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>
<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Transfer Rounding" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="true" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="false" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>
<WSAPunchRoundRule OutPunchLateChangePoint="0:00" UnscheduledInGrace="0:00" OutPunchEarlyInsideRound="0:01" OutPunchEarlyOutsideGrace="0:00" Name="Workrule Rounding" OutPunchEarlyOutsideRound="0:01" UseScheduledOut="false" InPunchLateInsideGrace="0:00" InPunchLateInsideRound="0:01" OutPunchLateOutsideGrace="0:00" InPunchEarlyInsideGrace="0:00" UnscheduledInRound="0:01" InPunchEarlyChangePoint="2:00" InPunchLateOutsideGrace="0:00" InPunchLateOutsideRound="0:01" OutPunchLateInsideGrace="0:00" IsMissedOutException="true" TransferGrace="0:00" UnscheduledOutGrace="0:00" TransferRound="0:01" InPunchLateChangePoint="0:00" OutPunchLateInsideRound="0:01" OutPunchLateOutsideRound="0:01" OutPunchEarlyInsideGrace="0:00" OutPunchEarlyChangePoint="0:00" InPunchEarlyOutsideRound="0:01" InPunchEarlyOutsideGrace="0:00" UnscheduledOutRound="0:01" InPunchEarlyInsideRound="2:00"/>
</Response>
</Kronos_WFC>`
我的班级是这样的
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Web;
using System.Xml.Serialization;
using System.Data;
using System.Dynamic;
using System.Collections;
using System.IO;
namespace Mojito.Models
{
public class KronosPunchRoundRuleSummary
{
public List<KronosPunchRoundRule> kronosPunchRoundRules { get; set; }
}
public class KronosPunchRoundRule
{
public virtual int KronosPunchRoundRuleId { get; set; }
public virtual string Name { get; set; }
public virtual string OutPunchLateChangePoint { get; set; }
public virtual string UnscheduledInGrace { get; set; }
public virtual string OutPunchEarlyInsideRound { get; set; }
public virtual string OutPunchEarlyOutsideGrace { get; set; }
public virtual string OutPunchEarlyOutsideRound { get; set; }
public virtual string UseScheduledOut { get; set; }
public virtual string InPunchLateInsideGrace { get; set; }
public virtual string InPunchLateInsideRound { get; set; }
public virtual string OutPunchLateOutsideGrace { get; set; }
public virtual string InPunchEarlyInsideGrace { get; set; }
public virtual string UnscheduledInRound { get; set; }
public virtual string InPunchEarlyChangePoint { get; set; }
public virtual string InPunchLateOutsideGrace { get; set; }
public virtual string InPunchLateOutsideRound { get; set; }
public virtual string OutPunchLateInsideGrace { get; set; }
public virtual bool IsMissedOutException { get; set; }
public virtual string TransferGrace { get; set; }
public virtual string UnscheduledOutGrace { get; set; }
public virtual string TransferRound { get; set; }
public virtual string InPunchLateChangePoint { get; set; }
public virtual string OutPunchLateInsideRound { get; set; }
public virtual string OutPunchLateOutsideRound { get; set; }
public virtual string OutPunchEarlyInsideGrace { get; set; }
public virtual string OutPunchEarlyChangePoint { get; set; }
public virtual string InPunchEarlyOutsideRound { get; set; }
public virtual string InPunchEarlyOutsideGrace { get; set; }
public virtual string UnscheduledOutRound { get; set; }
public virtual string InPunchEarlyInsideRound { get; set; }
}
}
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 xml-deserialization