在C#MVC中将XML数据绑定到模型

本文关键字:模型 数据绑定 XML C#MVC 中将 | 更新日期: 2023-09-27 18:21:34

我希望在C#MVC应用程序中将XML绑定到Model。

XML:

<people>  
    <person>
        <name>Mr Brown</name>
        <age>40</age>
        <hobby>
            <title>Eating</title>
            <description>eats a lot</description>
        </hobby>
        <hobby>
            <title>Sleeping</title>
            <description>sleeps a lot</description>
        </hobby>
    </person>
    <person>
        <name>Mr White</name>
        <age>40</age>
        <hobby>
            <title>Skiing</title>
            <description>more details on this</description>
        </hobby>
        <hobby>
            <title>Football</title>
            <description>watches football</description>
        </hobby>
    </person>
</people>

型号:

public class People
{
    public string Name { get; set; }
    public string Age { get; set; }
    public IList<Hobbies> Hobby {get; set; }
}
public class Hobbies
{
    public string Title { get; set; }
    public string Description { get; set; }
}

断开绑定:

var person = from a in xml.Descendants("person")
select new People 
{
    Name = a.Element("name").Value,
    Age = a.Element("age").Value,
    Hobby = *WHAT GOES HERE?*
}

我是C#的新手,正在寻找将XML中的数据绑定到personvar的最佳方法。稍后我将对其进行循环,并在HTML表中输出。

任何帮助都会很棒。

在C#MVC中将XML数据绑定到模型

你必须这样做:

var person = from a in xml.Descendants("person")
              select new People 
              {
                Name = a.Element("name").Value,
                Age = a.Element("age").Value,
                Hobby = a.Descendants("hobby")
                          .Select(x=> new Hobbies
                                       {
                                         Title =x.Element("title").Value,
                                         Description = x.Element("description").Value
                                       }).ToList()
               };

工作小提琴:

https://dotnetfiddle.net/2uKdd5

看起来您想要标准的XML反序列化。关于最好的方法,这里有一些好的答案

我会使用XmlSerializer从Xml加载并保存到Xml。您可以从这个类派生People,例如(SerializeManagement):

public class SerializeManagement<T>
{
    public static T ReadFromXML(string iPath)
     {
        T val = default(T);
        try
        {
            // load from XML
            using (var sw = new StreamReader(iPath, Encoding.Default))
            {
                var ser = new XmlSerializer(typeof(T));
                val = (T)ser.Deserialize(sw);
            }
        }
        catch
        {
            Console.WriteLine("Problem reading from xml data file.");
        }
        return val;
    }
    public void SaveToXML(string iPath)
    {
        try
        {
            //TODO => using
            var sw = new StreamWriter(iPath, false, Encoding.Default);
            var ser = new XmlSerializer(typeof(T));
            ser.Serialize(sw, this);
            sw.Close();
        }
        catch
        {
            Console.WriteLine("Problem saving to xml data file.");
        }
    }
}

如果您遇到问题,这可能是因为您的模型定义或xml结构:

然后你可以:

1)使用xsd实用程序从xml生成c#类;

2)使用SaveToXML从现有类生成XML。这样,您就可以确保XML结构符合您的模型。

享受吧!