如何使用 LINQ to XML 查询日期时间值?
我正在 silverlight 中开发 Windows Phone 7 应用程序。我是银光新手。我也是 LINQ to XML 的新手。在我的应用程序中,用户选择日期和时间。将一些交易详细信息提交到应用程序中。详细信息存储在 XML 文件中。我在应用程序中使用自定义日期控件进行日期选择,如下所示
private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
AppObj = Application.Current as App;
AppObj.date = (DateTime)EntryDate.Value;
}
然后 AppObj.date 的值存储在 XML 文件中。有时我使用 DateTime.Now 将日期存储在 XML 文件中。现在我想通过 LINQ to XML 查询来生成已提交交易详细信息的报告。我想生成今天、本周和当天的报告。本月。对于今天的日期报告,我使用以下代码
public void GetTransactionObjects(String strXMLFile, DateTime VDateTime)
{
XDocument doc = null;
XMLFileManager XMLDocObj = new XMLFileManager();
doc = XMLDocObj.LoadXMLFile(strXMLFile);
var vTransaction = from s in doc.Descendants("Transaction")
.Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
select new Transaction(s);
this.Clear();
AddRange(vTransaction);
}
Transaction 类包含以下构造函数。
public Transaction(XElement xElement)
{
Transaction_ID = Convert.ToInt32(xElement.Element("Transaction_ID").Value.ToString());
TransactionType_ID = Convert.ToInt32(xElement.Element("TransactionType_ID").Value.ToString());
Alphabet_ID = Convert.ToInt32(xElement.Element("Alphabet_ID").Value.ToString());
ID = Convert.ToInt32(xElement.Element("ID").Value.ToString());
SubCategory_ID = Convert.ToInt32(xElement.Element("SubCategory_ID").Value.ToString());
Item_ID = Convert.ToInt32(xElement.Element("Item_ID").Value.ToString());
Currency_ID = Convert.ToInt32(xElement.Element("Currency_ID").Value.ToString());
InputTypeMethod_ID = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Principle = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Interest = Convert.ToInt32(xElement.Element("Interest").Value.ToString());
ROI = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Amount = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Current_Date = Convert.ToDateTime(xElement.Element("Current_Date").Value.ToString());
}
在 XML 文件中,存储日期和日期的值。时间。该值的存储方式如下:
<Transactions>
<Transaction>
<Transaction_ID>0</Transaction_ID>
<TransactionType_ID>0</TransactionType_ID>
<Alphabet_ID>3</Alphabet_ID>
<ID>0</ID>
<SubCategory_ID>0</SubCategory_ID>
<Item_ID>0</Item_ID>
<Currency_ID>3</Currency_ID>
<InputTypeMethod_ID>0</InputTypeMethod_ID>
<Principle>0</Principle>
<Interest>0</Interest>
<ROI>0</ROI>
<Amount>5000</Amount>
<Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>
</Transaction>
</Transactions>
查看节点
<Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>
日期格式为 yyyy-mm-dd。
现在我应该如何编写以下查询来获取今天日期的所有提交的交易详细信息?
var vTransaction = from s in doc.Descendants("Transaction")
.Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
select new Transaction(s);
同样,我应该如何编写查询来获取本周和本周的所有交易详细信息。本月?您能给我提供任何可以解决上述问题的代码或链接吗?如果我做错了什么,请指导我。
I am developing window phone 7 application in silverlight. I am new to the silverlight. I am also new to LINQ to XML. In my application the user select the date & submit some transaction details into the application. The details gets stored in XML File. I am using the custom date control in my application for the date selection as follows
private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
AppObj = Application.Current as App;
AppObj.date = (DateTime)EntryDate.Value;
}
Then the value of AppObj.date gets stored in the XML file. Sometimes I use the DateTime.Now to store the date in the XML File. Now I want to generate the report of submitted transaction details by querying through LINQ to XML. I want to generate the report for today's date, current week & current month. For today's date report I am using the following code
public void GetTransactionObjects(String strXMLFile, DateTime VDateTime)
{
XDocument doc = null;
XMLFileManager XMLDocObj = new XMLFileManager();
doc = XMLDocObj.LoadXMLFile(strXMLFile);
var vTransaction = from s in doc.Descendants("Transaction")
.Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
select new Transaction(s);
this.Clear();
AddRange(vTransaction);
}
The Transaction class contains the following constructor.
public Transaction(XElement xElement)
{
Transaction_ID = Convert.ToInt32(xElement.Element("Transaction_ID").Value.ToString());
TransactionType_ID = Convert.ToInt32(xElement.Element("TransactionType_ID").Value.ToString());
Alphabet_ID = Convert.ToInt32(xElement.Element("Alphabet_ID").Value.ToString());
ID = Convert.ToInt32(xElement.Element("ID").Value.ToString());
SubCategory_ID = Convert.ToInt32(xElement.Element("SubCategory_ID").Value.ToString());
Item_ID = Convert.ToInt32(xElement.Element("Item_ID").Value.ToString());
Currency_ID = Convert.ToInt32(xElement.Element("Currency_ID").Value.ToString());
InputTypeMethod_ID = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Principle = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Interest = Convert.ToInt32(xElement.Element("Interest").Value.ToString());
ROI = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Amount = Convert.ToInt32(xElement.Element("InputTypeMethod_ID").Value.ToString());
Current_Date = Convert.ToDateTime(xElement.Element("Current_Date").Value.ToString());
}
In the XML File the value gets stored for date & time. The value gets stored as follows
<Transactions>
<Transaction>
<Transaction_ID>0</Transaction_ID>
<TransactionType_ID>0</TransactionType_ID>
<Alphabet_ID>3</Alphabet_ID>
<ID>0</ID>
<SubCategory_ID>0</SubCategory_ID>
<Item_ID>0</Item_ID>
<Currency_ID>3</Currency_ID>
<InputTypeMethod_ID>0</InputTypeMethod_ID>
<Principle>0</Principle>
<Interest>0</Interest>
<ROI>0</ROI>
<Amount>5000</Amount>
<Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>
</Transaction>
</Transactions>
Look at the node
<Current_Date>2010-12-31T18:08:23.433+05:30</Current_Date>
The date format is yyyy-mm-dd.
Now how should I write the following query to get all the submitted transaction details for today's date ?
var vTransaction = from s in doc.Descendants("Transaction")
.Where(x => x.Element("Current_Date").Value == VDateTime.ToShortDateString())
select new Transaction(s);
Similarly how should I write the query to get all the transaction details for the current week & current month? Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将
Convert.ToDateTime
或ToShortDateString
等与 LINQ to XML 一起使用。使用XAttribute
和XElement
中已存在的转换。例如:(您也应该在
Transaction
构造函数中使用转换运算符。)Don't use
Convert.ToDateTime
orToShortDateString
etc with LINQ to XML. Use the conversions which already exist inXAttribute
andXElement
. For example:(You should use the conversion operator in your
Transaction
constructor too.)