【问题标题】:Import Xml File and Bind to Object导入 Xml 文件并绑定到对象
【发布时间】: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


    【解决方案1】:

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

    希望有所帮助。问候!

    更新:

    要序列化未知对象,您必须将所有对象添加到序列化程序中,以说明必须序列化:

    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 方法。

    【讨论】:

    • 我已经尝试过这些教程 - 这是我从中获得大部分想法的地方。我似乎无法让它工作
    • 可能是因为您将反序列化一个您不知道是哪个类型的 XML。试试更新的代码。
    • 谢谢,这看起来更接近我真正想要做的事情,但它有点超出我的想象。我想尝试测试和调试,以便更清楚地了解它在做什么。我似乎被困在如何调用它并传递字符串和 iEnumerable 上。字符串的传递似乎很简单,但不确定我是否理解如何传递 Ienumerable
    • 所以这就是我一直在尝试但收到错误的方法
      public ActionResult Test() { string fileName = @"c:\WSAPunchRoundRule.xml"; KronosPunchRoundRuleSummary kprrs = new KronosPunchRoundRuleSummary(); KronosPunchRoundRule.Load(fileName,kprrs);返回视图(); }' 错误 4 参数 2:无法从 'Mojito.Models.KronosPunchRoundRuleSummary' 转换为 'System.Collections.Generic.IEnumerable' 错误
    • 因为你的 KronosPunchRoundRule 是一个 List 并且在方法中是一个 IEnumerable。您可以将 List 更改为 IEnumerable,或将 IEnumerable 更改为 List。或者你可以使用IEnumerbale“tolist”的功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多