简体   繁体   English

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

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

I have a C# MVC application which I need to output the following data to a 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>

The above data comes from a SQL table/column which stores the data as a XML data type. 上面的数据来自一个SQL表/列,该表将数据存储为XML数据类型。 Can somone give me a code example to extract the values of the elements(maybe assign each value to variable) so I can pass it to a view? Somone可以给我一个代码示例来提取元素的值(也许将每个值分配给变量),以便将其传递给视图吗?

So I need to get the values "true" , "City", "Demme" , "http://test1.com", "http://test3.com/img1....and so on. 因此,我需要获取值“ true”,“ City”,“ Demme”,“ http://test1.com”,“ http://test3.com/img1 ....等等”。

Whats the best way to present this data to a view? 将数据呈现给视图的最佳方法是什么?

My idea is to create classes, corresponding to your Xml file, a Version class, a Factory class. 我的想法是创建与您的Xml文件相对应的类,Version类,Factory类。 Load the xml file, and then pass it to your class that return your data, here is how I do it : 加载xml文件,然后将其传递给返回您的数据的类,这是我的操作方法:

The Version class : 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;
    }
}

The Factory class : 工厂类:

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

And last, in your MCV Controller : 最后,在您的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;
           }
       }

Now you have a list of values. 现在您有了值列表。 Assign it to the model and then use the model to populate the view. 将其分配给模型,然后使用模型填充视图。

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

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