繁体   English   中英

C#MVC:如何从XML提取值并传递给视图

[英]C# MVC: How extract values from XML and pass to view

我有一个C#MVC应用程序,需要将以下数据输出到视图:

            <versions>
              <product>true</product>
              <type>city</type>
              <factory name="Demme" url="http://test1.com" thumbnail="http://test3.com/img1" interval="10" />
              <factory name="Vollick" url="http://test2.com" thumbnail="http://test3.com/img1" interval="10" />
              <factory name="Tony" url="http://test3.com" thumbnail="http://test3.com/img1" interval="10" />
            </versions>

上面的数据来自一个SQL表/列,该表将数据存储为XML数据类型。 Somone可以给我一个代码示例来提取元素的值(也许将每个值分配给变量),以便将其传递给视图吗?

因此,我需要获取值“ true”,“ City”,“ Demme”,“ http://test1.com”,“ http://test3.com/img1 ....等等”。

将数据呈现给视图的最佳方法是什么?

我的想法是创建与您的Xml文件相对应的类,Version类,Factory类。 加载xml文件,然后将其传递给返回您的数据的类,这是我的操作方法:

Version类:

public class Version
{
    public bool IsProduct { get; set; }
    public string City { get; set; }
    public List<Factory> Factories { get; set; }

    //Create a version
    public Version(XElement xVersion)
    {
        IsProduct = Convert.ToBoolean(xVersion.Element("Product").Value);
        City = xVersion.Element("City").Value;
        Factories = Factory.GetFactories(xVersion);
    }

    //Get the list of versions
    public static List<Version> GetVersions(XElement xDocument)
    {
        if (xDocument == null)
            return null;

        List<Version> list = new List<Version>();
        var xVersions = xDocument.Elements("Version");

        foreach (var xVersion in xVersions)
        {
            list.Add(new Version(xVersion));
        }

        return list;
    }
}

工厂类:

public class Factory
{
    public string Name { get; set; }
    public string Url { get; set; }
    public string Thumbnail { get; set; }
    public string Interval { get; set; }

    //Create a factory
    public Factory(XElement xFactory)
    {
        Name = xFactory.Attribute("Name").Value;
        Url = xFactory.Attribute("Url").Value;
        Thumbnail = xFactory.Attribute("Thumbnail").Value;
        Interval = xFactory.Attribute("Interval").Value;
    }

    //Get the factories of a version
    public static List<Factory> GetFactories(XElement xVersion)
    {
        var xFactories = xVersion.Elements("Factory");
        if (xFactories == null)
            return null;

        List<Factory> list = new List<Factory>();

        foreach (var xFactory in xFactories)
        {
            list.Add(new Factory(xFactory));
        }

        return list;
    }
}

最后,在您的MCV控制器中:

private void myMethod()
    {
        var xDocument = XElement.Load("XmlFilePath");
        var versions = Version.GetVersions(xDocument);

        //And then, pass the -versions- to your typed view ^^
    }
using System.Xml;
    List<string> values= new List<string>();    
    XmlTextReader reader = new XmlTextReader ("books.xml");
    while (reader.Read()) 
    {
           switch (reader.NodeType) 
           {
                   while (reader.MoveToNextAttribute()) // Read the attributes.
                     values.add(reader.Value);
                   break;
         case XmlNodeType.Text: //Display the text in each element.
                     values.add(reader.Value);
                   break;
         case XmlNodeType. EndElement: //Display the end of the element.
          Console.WriteLine(">");
                   break;
           }
       }

现在您有了值列表。 将其分配给模型,然后使用模型填充视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM