gpt4 book ai didi

c# - 从 App_Data 中的文件获取 XML 数据并将其转换为 IQueryable 的最有效方法?

转载 作者:行者123 更新时间:2023-11-30 21:55:41 25 4
gpt4 key购买 nike

我正在将现有的 WebForms 应用程序转换为 MVC5。有数据存储在 /App_Data 文件夹中的 XML 文件中。我创建了一个包含所有匹配项的模型。我需要从此 XML 文件中获取数据,然后将其作为 IQueryable 返回。

最有效/最佳实践方法是什么?任何简单的例子都会很棒。

澄清我知道从 XML 获取数据的各种方法。问题更多地围绕着将数据放入 IQueryable。

这是我的模型:

using System;
using System.ComponentModel.DataAnnotations;

namespace Framework.Models
{
public class NewsModel
{
public NewsModel()
{
Title = string.Empty;
Summary = string.Empty;
Image = string.Empty;
ImageWidth = 0;
ImageHeight = 0;
ImageSrcSet = string.Empty;
ImageSizes = string.Empty;
Url = new Uri("#");
UrlText = string.Empty;
UrlTarget = "_self";
Date = DateTime.Now;
}

[Display(Name = "Title")]
public String Title { get; set; }

[Display(Name = "Summary")]
public String Summary { get; set; }

[Display(Name = "Image")]
public String Image { get; set; }

[Display(Name = "ImageWidth")]
public int ImageWidth { get; set; }

[Display(Name = "ImageHeight")]
public int ImageHeight { get; set; }

[Display(Name = "ImageSrcSet")]
public String ImageSrcSet { get; set; }

[Display(Name = "ImageSizes")]
public String ImageSizes { get; set; }

[Display(Name = "Url")]
public Uri Url { get; set; }

[Display(Name = "UrlText")]
public String UrlText { get; set; }

[Display(Name = "UrlTarget")]
public String UrlTarget { get; set; }

[Display(Name = "Date")]
public DateTime Date { get; set; }

}
}

这是我的 XML 文档:

<?xml version="1.0" encoding="utf-8" ?>
<News>
<Item>
<Title><![CDATA[Example Title 01]]></Title>
<Summary>
<![CDATA[
Example Text 01
]]>
</Summary>
<Image><![CDATA[]]></Image>
<ImageWidth>1420</ImageWidth>
<ImageHeight>480</ImageHeight>
<ImageAlignment>left</ImageAlignment>
<ImageSrcSet><![CDATA[]]></ImageSrcSet>
<ImageSizes><![CDATA[]]></ImageSizes>
<Url><![CDATA[]]></Url>
<UrlText><![CDATA[]]></UrlText>
<UrlTarget>_blank</UrlTarget>
<Date><![CDATA[05/01/2009]]></Date>
</Item>
<Item>
<Title><![CDATA[Example Title 02]]></Title>
<Summary>
<![CDATA[
Example Text 02
]]>
</Summary>
<Image><![CDATA[]]></Image>
<ImageWidth>920</ImageWidth>
<ImageHeight>480</ImageHeight>
<ImageAlignment>left</ImageAlignment>
<ImageSrcSet><![CDATA[]]></ImageSrcSet>
<ImageSizes><![CDATA[]]></ImageSizes>
<Url><![CDATA[]]></Url>
<UrlText><![CDATA[]]></UrlText>
<UrlTarget>_self</UrlTarget>
<Date><![CDATA[05/01/2009]]></Date>
</Item>
</News>

最佳答案

这会让你开始

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication43
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(Framework.Models.News));
XmlTextReader reader = new XmlTextReader(FILENAME);
Framework.Models.News news = (Framework.Models.News)xs.Deserialize(reader);
}

}

}
namespace Framework.Models
{

[XmlRoot("News")]
public class News
{
[XmlElement("Item")]
public List<NewsModel> newsModel { get; set; }
}
[XmlRoot("Item")]
public class NewsModel
{
public NewsModel()
{
Title = string.Empty;
Summary = string.Empty;
Image = string.Empty;
ImageWidth = 0;
ImageHeight = 0;
ImageSrcSet = string.Empty;
ImageSizes = string.Empty;
Url = "#";
UrlText = string.Empty;
UrlTarget = "_self";
//Date = DateTime.Now;
}

[XmlElement("Title")]
public String Title { get; set; }

[XmlElement("Summary")]
public String Summary { get; set; }

[XmlElement("Image")]
public String Image { get; set; }

[XmlElement("ImageWidth")]
public int ImageWidth { get; set; }

[XmlElement("ImageHeight")]
public int ImageHeight { get; set; }

[XmlElement("ImageSrcSet")]
public String ImageSrcSet { get; set; }

[XmlElement("ImageSizes")]
public String ImageSizes { get; set; }

[XmlElement("Url")]
public string Url { get; set; }

[XmlElement("UrlText")]
public String UrlText { get; set; }

[XmlElement("UrlTarget")]
public String UrlTarget { get; set; }

[XmlElement("Date")]
public string Date { get; set; }

}
}

关于c# - 从 App_Data 中的文件获取 XML 数据并将其转换为 IQueryable<MyModel> 的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com