[英]How to Store data without using Database and how to retrieve them?
我正在解析html文件以通过列名提取表格信息。 我希望让用户为列名提供输入。 并根据该列名称将提取表格信息。
现在,用户将输入的列名称,根据该列名称,我想从html文件中找到表格信息。
但是我应该在哪里存储用户输入的列名? 以及如何检索它们? 我不想使用数据库。
编辑 :我们如何在xml文件中存储数据以及如何从中检索数据以及如何更新xml文件以存储数据? 就像我有3个列名称Name, Address,Phone no
,对于该用户,将输入可能的列名,如Nm,Add,PhNo.
和Nam,Addre,PhoNo
等。
很多人一直在谈论XML,这是一个好主意。 但是,如果是的LINQ提供给你,你真的 shoudl考虑使用LINQ到XML ,而不是SAX / DOM解析。
与SAX和DOM解析器相比,Linq to XML使得解析,创建和编辑XML文件变得更容易。 使用SAX / DOM解析通常需要大量循环来获取正确的元素或通过节点导航。
从MSDN获取的示例:
使用DOM解析:
XmlDocument doc = new XmlDocument();
XmlElement name = doc.CreateElement("Name");
name.InnerText = "Patrick Hines";
XmlElement phone1 = doc.CreateElement("Phone");
phone1.SetAttribute("Type", "Home");
phone1.InnerText = "206-555-0144";
XmlElement phone2 = doc.CreateElement("Phone");
phone2.SetAttribute("Type", "Work");
phone2.InnerText = "425-555-0145";
XmlElement street1 = doc.CreateElement("Street1");
street1.InnerText = "123 Main St";
XmlElement city = doc.CreateElement("City");
city.InnerText = "Mercer Island";
XmlElement state = doc.CreateElement("State");
state.InnerText = "WA";
XmlElement postal = doc.CreateElement("Postal");
postal.InnerText = "68042";
XmlElement address = doc.CreateElement("Address");
address.AppendChild(street1);
address.AppendChild(city);
address.AppendChild(state);
address.AppendChild(postal);
XmlElement contact = doc.CreateElement("Contact");
contact.AppendChild(name);
contact.AppendChild(phone1);
contact.AppendChild(phone2);
contact.AppendChild(address);
XmlElement contacts = doc.CreateElement("Contacts");
contacts.AppendChild(contact);
doc.AppendChild(contacts);
使用Linq到XML:
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
更容易做,更清晰。
编辑:
将XML树保存到contacts.xml:
// using the code above
contact.Save("contacts.xml");
加载contacts.xml文件:
//using the code above
XDocument contactDoc = XDocument.Load("contacts.xml");
要更新树的元素, doc中有一些函数可以根据您的要求执行此操作
也许您可以序列化DataTable,但请记住,如果用数据填充它可能会变得很大并且需要花费大量时间来序列化或反序列化。 如果以其他方式存储数据,您可能希望将列信息存储在配置/项目文件中。
HTH,-sa
使用数据库。 如果您不想设置客户端/服务器体系结构的麻烦,请使用SQLite ,它具有多个.Net版本或SQL Server Express 。
数据库的替代方案是: http : //www.filehelpers.com/
如果它只是十列怎么样在App.Config中存储为分隔字符串?
如果你必须选择数据库; 然后我会使用Xml,当文件变大时使用SAX进行解析。
了解如何使用C#操作XML文件数据
- 编辑 -
这是对您的评论的回应。 让我们假设您有一个具有以下控件的屏幕:
TextBox
为txtColumnName; 当用户提供列名并单击“添加”按钮时,将其另存为xml属性。 ComboBox
为cbxColumns; 选择当前列 TextBox
为txtValue; 如果提供了此选项,则将值保存在“列”组合中选择的列中。 请通过示例链接,它将帮助您在运行时添加/删除/更新节点。 你可以使用你的XmlDatabaseWrapper
类来包装所有的CRUD操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.