我在xml文件中委托数据,但它没有以正确的方式进行,因为当我检索我的数据时,它只检索一部分数据。
您能帮我解决一下如何输入数据,检索整个数据以及比较我在文本框中输入的具体数据。???
这是我的代码: 这是用于委托数据。:
string curFile = @"D:\5 Semester\Project\VP Project\VP Project\bin\Debug\New Entry.xml";
string s1 = textBox1.Text;
string s2 = textBox2.Text;
string s3 = comboBox3.Text;
string s4 = comboBox1.Text;
string s5 = comboBox2.Text;
if (File.Exists(curFile) && curFile!=null)
{
try
{
var library = XElement.Load("New Entry.xml");
library.Add(new XAttribute("FirstName",s1));
library.Add(new XElement("First", new XAttribute("Name", s1)));
library.Add(new XElement("Last", new XAttribute("Name", s2)));
library.Add(new XElement("Profession", new XAttribute("Type", s3)));
library.Add(new XElement("Department", new XAttribute("Degree", s4)));
library.Add(new XElement("Semester", new XAttribute("No", s5)));
library.Save("New Entry.xml");
MessageBox.Show("Data Appended!");
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
else if (!(File.Exists(curFile)))
{
using (XmlWriter writer = XmlWriter.Create("New Entry.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("New_Entry");
writer.WriteElementString("FirstName", s1);
writer.WriteElementString("LastName", s2);
writer.WriteElementString("Profession", s3);
writer.WriteElementString("Department", s4);
writer.WriteElementString("Semester", s5);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
MessageBox.Show("File Created!");
}
}
else
{
MessageBox.Show("File Cannot be Created!");
}
这是用于检索数据:
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"D:\5 Semester\Project\VP Project\VP Project\bin\Debug\New Entry.xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
nav.MoveToFirstChild();
do
{
//Find the first element.
if (nav.NodeType == XPathNodeType.Element)
{
//Determine whether children exist.
if (nav.HasChildren == true)
{
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all the children.
listBox1.Items.Add("All Record ");
do
{
//Display the data.
listBox1.Show();
listBox1.Items.Add(nav.Value);
}
while (nav.MoveToNext());
}
}
}
while (nav.MoveToNext());
}
但它不是检索整个数据....我怎样才能比较存储在xml中的数据。我在文本框和xml文件数据中输入的数据如果匹配,则显示后面的数据。
答案 0 :(得分:-1)
首先:如果XML文件与.exe应用程序位于同一文件夹中,则不需要完整路径,只需使用(“file.xml”); 处理xml文件最好和最简单的方法是这样的: 您将需要构建一个类,以便您可以定义字段,属性并构建构造函数。
List<yourclass> data= new List<yourclass>();
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlNodeList list= doc["your root"].ChildNodes;
foreach (XmlNode node in list)
{
//get all child nodes inner text
string[] info = { clt["info1"].InnerText, clt["info2"].InnerText, clt["info3"].InnerText, clt["info4"].InnerText, aslong as it is };
//ad all info to your class list
data.Add(new client(info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7], info[8],info[9]));
}
return data;
并保存:
您将需要像以前一样加载XML文档,然后创建新节点并填充它们,然后获取您需要附加的节点或创建新节点并将其附加到根节点。
doc.Load("file.xml");
XmlNode any= doc.CreateElement("elementname");
any.InnerText = textsource.ToString();
targetnode.AppendChild(any);
rootnode.AppendChild(targetnode);
doc.Save("file.xml");