C#MVC:如何从XML中提取值并传递给视图
我有一个C#MVC应用程序,我需要将以下数据输出到视图:C#MVC:如何从XML中提取值并传递给视图,c#,C#,我有一个C#MVC应用程序,我需要将以下数据输出到视图: <versions> <product>true</product> <type>city</type> <factory name="Demme" url="http://test1.com" thumbnail="http://test3.com/img1" in
<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能给我一个代码示例来提取元素的值(可能会将每个值赋给变量),这样我就可以将它传递给视图吗
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;
}
}
所以我需要得到“true”、“City”、“Demme”等值。“http://test1.com", "http://test3.com/img1....and 等等
向视图显示此数据的最佳方式是什么?使用System.Xml;
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;
}
}
列表值=新列表();
XmlTextReader=newxmltextreader(“books.xml”);
while(reader.Read())
{
开关(reader.NodeType)
{
while(reader.MoveToNextAttribute())//读取属性。
values.add(reader.Value);
打破
case XmlNodeType.Text://显示每个元素中的文本。
values.add(reader.Value);
打破
case XmlNodeType.EndElement://显示元素的结尾。
Console.WriteLine(“>”);
打破
}
}
现在您有了一个值列表。将其分配给模型,然后使用模型填充视图。使用System.Xml;
列表值=新列表();
XmlTextReader=newxmltextreader(“books.xml”);
while(reader.Read())
{
开关(reader.NodeType)
{
while(reader.MoveToNextAttribute())//读取属性。
values.add(reader.Value);
打破
case XmlNodeType.Text://显示每个元素中的文本。
values.add(reader.Value);
打破
case XmlNodeType.EndElement://显示元素的结尾。
Console.WriteLine(“>”);
打破
}
}
现在您有了一个值列表。将其分配给模型,然后使用模型填充视图。我的想法是创建与您的Xml文件相对应的类,一个版本类,一个工厂类。加载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;
}
}
我的想法是创建与Xml文件、版本类和工厂类相对应的类。加载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;
}
}
你在解析这个xml时有什么特别的问题吗?或者你甚至没有尝试过学习如何做?你不能用C语言从xml中提取值。这是不可能的。你需要使用C或Java:)PS:你试过什么了吗?你有什么代码可以展示吗?你有“C”xml吗“Google上的点击率为零???这与您的另一个相同数据结构的问题不同吗?XML在2002年被取消,因此您的数据被卡住。可能的重复您在解析此XML时有特定的问题,或者您甚至没有尝试学习如何做?无法从C#中的XML中提取值。这是不可能的。你需要使用C或Java:)PS:你试过什么了吗?你有什么代码可以显示吗?“C#XML”在谷歌上的点击率为零吗???这与你的另一个相同数据结构的问题不同吗?XML在2002年被取消,所以你的数据被卡住了。可能是重复的,谢谢你的帮助。我很感激。现在实现这一点,我几乎做到了,但有一个问题。XML数据来自SQL数据库,而不是XElement.Load(“XmlFilePath”)文件;抛出并出错“路径中的非法字符。我使用了XElement.Parse而不是XElement.Load,我不再收到错误。现在让我看看这是否有效。我调试了我的程序,我可以实际看到传递给Getversion()的xDocument中的数据;但是var xVersions=xDocument.Elements(“版本”)您的Xml文件是否只包含一个元素“Versions”?如果是,请使用xDocument.element(“Versions”)。另外,请确保元素名称(“Versions”或“Version”),这就是数据为空的原因,我想谢谢你的帮助。我很感激。现在实现这一点,我几乎解决了一个问题。XML数据来自SQL数据库,而不是一个文件,因此XElement.Load(“XmlFilePath”);抛出并出错”路径中存在非法字符。我使用了XElement.Parse而不是XElement.Load,因此不再出现错误。现在让我看看这是否有效。我调试了我的程序,实际上可以看到传递给Getversion()的XDocument中的数据;但是,var xVersions=xDocument.Elements(“Version”)为空您的Xml文件是否只包含一个元素“Versions”?如果是这样,请改用xDocument.Element(“版本”)。另一件事,确保元素名(“Versions”或“Version”),这就是我认为数据为空的原因