导入Xml文件并绑定到对象

本文关键字:对象 绑定 Xml 文件 导入 | 更新日期: 2023-09-27 17:49:33

我想导入一个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();
 }

我也尝试过这个enter 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>`

My Class看起来像这样

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; }
}   

}

导入Xml文件并绑定到对象

我建议您阅读XML序列化器和反序列化器教程。

  • XML序列化和反序列化:第1部分
  • 序列化/反序列化任何对象到XML文件

希望帮助。问候!

更新:

要序列化一个未知对象,你必须向序列化器传递所有对象的一个add,以说明必须序列化的对象:

public static void Save<T>(T item, string filename, IEnumerable<Type> typeList) where T : class, new()
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());
        // To write to a file, create a StreamWriter object.
        StreamWriter writer = null;
        try
        {
            writer = new StreamWriter(filename);
            var ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, string.Empty);
            xmlSerializer.Serialize(writer, item, ns);
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (writer != null)
                writer.Close();
        }
    }

反序列化未知对象的方法相同:

public static T Load<T>(string filename, IEnumerable<Type> typeList) where T : class, new()
    {
        if (!File.Exists(filename))
            return new T();
        TextReader fileStream = null;
        try
        {
            // Construct an instance of the XmlSerializer with the type
            // of object that is being deserialized.
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());
            // To read the file, create a FileStream.
            fileStream = new StreamReader(filename);
            return xmlSerializer.Deserialize(fileStream) as T;
            // Call the Deserialize method and cast to the object type.
            //  return xmlSerializer.Deserialize(fileStream) as T;
        }
        catch (Exception ex)
        {
            return new T();
        }
        finally
        {
            if (fileStream != null)
                fileStream.Close();
        }
    }

更新2:

T ->是要序列化的对象。

filname ->您要保存的路径/目的地

IEnumerable ->是一个类型列表。只声明一个IEnumerable,然后添加你要序列化的所有类型,然后传递给Save, Load方法。