C#。帮助表单XML
编辑: 好了,终于开始工作了。如果有人需要,这是我的代码C#。帮助表单XML,c#,xml,C#,Xml,编辑: 好了,终于开始工作了。如果有人需要,这是我的代码 public void Save(string savePath, string[] folderPath, string date, string time) { //path for xml //TODO: use "+savePath+" string fileName = date+"_session.xml"; if (File.Exists(fi
public void Save(string savePath, string[] folderPath, string date, string time)
{
//path for xml
//TODO: use "+savePath+"
string fileName = date+"_session.xml";
if (File.Exists(fileName) == false)
{
//ID
int sessionId = 1;
//creating
XDocument doc = new XDocument(
new XElement("sessions",
new XElement("session",
new XAttribute("id", sessionId++),
new XAttribute("amount", folderPath.Length),
new XAttribute("date", date),
new XAttribute("time", time))));
XElement folders = doc.Descendants("session").FirstOrDefault();
for (int i = 0; i < folderPath.Length; i++)
{
folders.Add(new[] { new XElement("folderPath", folderPath[i]) });
}
//saving document
doc.Save(fileName);
} else
{
XDocument doc = XDocument.Load(fileName);
int maxId = doc.Root.Elements("session").Max(t => Int32.Parse(t.Attribute("id").Value));
XElement data = new XElement("session",
new XAttribute("id", ++maxId),
new XAttribute("amount", folderPath.Length),
new XAttribute("date", date),
new XAttribute("time", time));
for (int i = 0; i < folderPath.Length; i++)
{
data.Add(new[] { new XElement("folderPath", folderPath[i]) });
}
doc.Root.Add(data);
doc.Save(fileName);
}
}
我创建了一个程序,可以将当前打开的文件夹保存到.txt文件中,
我正试图以以下方式形成一个XML
19.07.2016
22:05
C:\\users\test
C:\\program data\\test2
C:\\users\\aleksei\\desktop
19.07.2016
23:15
C:\\users\test6
C:\\users\\aleksei\\pictures
这就是我喜欢创建新xml文档的方式
string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<!--This file is generated by the program.-->" +
"<Folders></Folders>";
XDocument doc = XDocument.Parse(header);
XElement folders = doc.Descendants("Folders").FirstOrDefault();
folders.Add( new[] {
new XElement("Amount", amount),
new XElement("Date", date),
new XElement("Time", time),
new XElement("folderPath", folderPath)
});
string header=“”+
"" +
"";
XDocument doc=XDocument.Parse(标题);
XElement folders=doc.substands(“folders”).FirstOrDefault();
文件夹。添加(新[]{
新XElement(“金额”,金额),
新XElement(“日期”,日期),
新元素(“时间”,时间),
新XElement(“folderPath”,folderPath)
});
我通常使用下面的方法使用LINQ创建XML文档
public static void Save(string savePath, string folderPath, string date, string time, string amount)
{
string count =" 3";
//I am explicitly initializing the id count but you need to query and sort in descedning order take the first id and increment accordingly
XDocument xDocument = XDocument.Load(savePath);
if (xDocument != null)
{
//To add the xml document at specific location
xDocument.Element("Folders").Elements("Save")
.Where(x => x.Attribute("Id").Value == count)
.FirstOrDefault()
.AddAfterSelf(
new XElement("Save", new XAttribute("Id", count+1),
new XElement("Amount", new XAttribute("amount", "3"),
new XElement("Date", date),
new XElement("Time", time),
new XElement("folder", folderPath)))
);
FileStream fs = new FileStream(savePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
xDocument.Save(fs);
}
else
{
//If folder is open and xml file doesnt exist
XDocument newDocument = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This file is generated by the program."),
//Root single Element
new XElement("Folders",
//child Elements
new XElement("Save", new XAttribute("Id", "1"),
new XElement("Amount", new XAttribute("amount", "3"),
new XElement("Date", date),
new XElement("Time", time),
new XElement("folder", folderPath)))
));
FileStream fs = new FileStream(savePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
newDocument.Save(fs);
}
在这里,您需要查询现有xml文档的ID属性,并按降序排列,然后如果有打开的文件夹,请在此ID之后添加新节点。当前代码有什么问题?什么不起作用?当前没有任何东西起作用,这里给我一个错误:XDocument XDocument=XDocument.Load(date+“_saves.xml”);这样说:System.Xml.dllI中发生了一个未处理的“System.Xml.XmlException”类型的异常,我甚至不知道我所做的是否正确。也许使用LINQ更好?您需要向Load方法传递有效路径。据我所知,
date
不是一个文件路径…文件名应该是19.07.2016\u save.xml。我不断收到错误消息,表示附加信息:访问路径“C:\Users\Public”被拒绝。或我选择的任何路径。以管理员身份运行Visula studio或授予文件夹的完全权限
public static void Save(string savePath, string folderPath, string date, string time, string amount)
{
string count =" 3";
//I am explicitly initializing the id count but you need to query and sort in descedning order take the first id and increment accordingly
XDocument xDocument = XDocument.Load(savePath);
if (xDocument != null)
{
//To add the xml document at specific location
xDocument.Element("Folders").Elements("Save")
.Where(x => x.Attribute("Id").Value == count)
.FirstOrDefault()
.AddAfterSelf(
new XElement("Save", new XAttribute("Id", count+1),
new XElement("Amount", new XAttribute("amount", "3"),
new XElement("Date", date),
new XElement("Time", time),
new XElement("folder", folderPath)))
);
FileStream fs = new FileStream(savePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
xDocument.Save(fs);
}
else
{
//If folder is open and xml file doesnt exist
XDocument newDocument = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This file is generated by the program."),
//Root single Element
new XElement("Folders",
//child Elements
new XElement("Save", new XAttribute("Id", "1"),
new XElement("Amount", new XAttribute("amount", "3"),
new XElement("Date", date),
new XElement("Time", time),
new XElement("folder", folderPath)))
));
FileStream fs = new FileStream(savePath,
FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
newDocument.Save(fs);
}