1

次のデータをビューに出力する必要がある 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>

上記のデータは、データを XML データ型として格納する SQL テーブル/列から取得されます。ビューに渡すことができるように、要素の値を抽出する(おそらく各値を変数に割り当てる)コード例を教えてもらえますか?

したがって、「true」、「City」、「Demme」、「http://test1.com」、「http://test3.com/img1....」などの値を取得する必要があります。

このデータをビューに表示する最良の方法は何ですか?

4

2 に答える 2

1

私の考えは、Xml ファイル、Version クラス、Factory クラスに対応するクラスを作成することです。xml ファイルをロードし、データを返すクラスに渡します。これが私のやり方です。

バージョン クラス:

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 Controller で:

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

        //And then, pass the -versions- to your typed view ^^
    }
于 2012-07-01T08:26:40.787 に答える
0
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;
           }
       }

これで、値のリストができました。それをモデルに割り当ててから、モデルを使用してビューに入力します。

于 2012-07-01T04:58:31.417 に答える