如何使用 LINQ to XML 在 XML 文件中写入第一条记录的 ID?
我正在开发 Windows Phone 7 应用程序。我使用 XML 作为我的应用程序的数据库。我的应用程序使用 XML 文件而不是数据库表。我的 xml 文件之一具有以下结构。
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
<Category>
<Category_ID></Category_ID>
<Category_Name></Category_Name>
<TransactionType_ID></TransactionType_ID>
</Category>
<Category>
<Category_ID></Category_ID>
<Category_Name></Category_Name>
<TransactionType_ID></TransactionType_ID>
</Category>
</Categories>
在上面的 XML 文件中,我插入了 Category_Name 和 Category_Name 的值。使用 LINQ to XML 通过我的移动应用程序的用户界面获取 TransactionType_ID。在上面的 XML 文件中,我将 Category_ID 视为主键 & TransactionType_ID 作为外键,属于其他 xml 文件。在上面的 XML 文件中,我想为节点 Category_ID 生成自动增量 id,就像我们在数据库表中所做的那样。为此,我使用以下代码
private int new_record_id()
{
IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream bookfile = new IsolatedStorageFileStream("Categories.xml", System.IO.FileMode.Open, isstore);
XDocument xmldetails = XDocument.Load(bookfile);
var details = (from detail in xmldetails.Descendants("Category")
select (int.Parse(detail.Element("Category_ID").Value))).Max();
bookfile.Close();
return details + 1;
}
,但是当我在 XML 文件中插入第一条记录时,下面的句子会出现错误,因为我的 XML 文件开头没有记录。
var details = (from detail in xmldetails.Descendants("Category")
select (int.Parse(detail.Element("Category_ID").Value))).Max();
您能否提供任何代码或链接或任何解决方案,通过它们我可以在 XML 文件中插入第一条记录?如果我做错了什么,请指导我。
I am developing window phone 7 application. I am using XML as a database for my application. I am using a XML file instead of a database table for my application. I having the following structure for one of my xml file.
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
<Category>
<Category_ID></Category_ID>
<Category_Name></Category_Name>
<TransactionType_ID></TransactionType_ID>
</Category>
<Category>
<Category_ID></Category_ID>
<Category_Name></Category_Name>
<TransactionType_ID></TransactionType_ID>
</Category>
</Categories>
In the above XML file I am inserting the values for Category_Name & TransactionType_ID through the user interface of my mobile application by using LINQ to XML. In the above XML file I am treating the Category_ID as a primary key & TransactionType_ID as a foreign key which belongs to other xml file. In the above XML file I want to generate the autoincrement id for the node Category_ID as we do in the database table. For this purpose I am using the following code
private int new_record_id()
{
IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream bookfile = new IsolatedStorageFileStream("Categories.xml", System.IO.FileMode.Open, isstore);
XDocument xmldetails = XDocument.Load(bookfile);
var details = (from detail in xmldetails.Descendants("Category")
select (int.Parse(detail.Element("Category_ID").Value))).Max();
bookfile.Close();
return details + 1;
}
But when I insert first record in the XML file then it gives error in the following sentence because there is no record in my XML file at the beginning.
var details = (from detail in xmldetails.Descendants("Category")
select (int.Parse(detail.Element("Category_ID").Value))).Max();
Can you please provide me any code or link or any solution through which I can insert the first record in the XML file ? If I am doing anything wrong then please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,首先您的查询可以更简单地编写为:
现在,如果您希望能够处理 XML 中没有书籍的情况,我将首先显式测试该情况:(
如果您愿意,也可以使用条件运算符。)
Well, to start with your query can be written much more simply as:
Now, if you want to be able to handle the case where there are no books in your XML, I'd test that explicitly first:
(You could use the conditional operator for this if you wanted, too.)