I have a C# MVC application which I need to output the following data to a view:
<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. 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?
So I need to get the values "true" , "City", "Demme" , "http://test1.com", "http://test3.com/img1....and so on.
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. Load the xml file, and then pass it to your class that return your data, here is how I do it :
The Version class :
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 :
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.