else
{
Notes.Elements().Last().LastNode.AddAfterSelf(new XElement(xmlProperties[i], propertyValues[i]));
}
}
}
else
{
Notes.AddFirst(new XElement(noteName, new XAttribute("No", 1)));
for (int i = 0; i < xmlProperties.Length; i++)
{
if (i == 0)
{
Notes.Element(noteName).AddFirst(new XElement(xmlProperties[i], propertyValues[i]));
}
else
{
Notes.Element(noteName).LastNode.AddAfterSelf(new XElement(xmlProperties[i], propertyValues[i]));
}
}
}
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw e;
//return false;
}
}
//添加记录:添加到末尾(方法二)
public bool AddNote(XElement newNote)
{
try
{
if (newNote.Elements().Count() == xmlProperties.Length)
{
if (Notes.Elements(noteName).Count() > 0)
{
int newNo;
var lastNote = from Note in Notes.Elements() select Convert.ToInt32(Note.Attribute("No").Value);
newNo = lastNote.Max() + 1;
if(newNote.Attribute("No") == null)
{
newNote.Add(new XAttribute("No", newNo));
}
else
{
newNote.Attribute("No").Value = newNo.ToString();
}
Notes.Elements().Last().AddAfterSelf(newNote);
}
else
{
if (newNote.Attribute("No") == null)
{
newNote.Add(new XAttribute("No", 1));
}
else
{
newNote.Attribute("No").Value = "1";
}
Notes.AddFirst(newNote);
}
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw e;
//return false;
}
}
//添加记录:添加到开头
public bool AddFistNote(XElement newNote)
{
try
{
if (newNote.Elements().Count() == xmlProperties.Length)
{
if (Notes.Elements(noteName).Count() > 0)
{
int newNo;
var lastNote = from Note in Notes.Elements() select Convert.ToInt32(Note.Attribute("No").Value);
newNo = lastNote.Max() + 1;
if (newNote.Attribute("No") == null)
{
newNote.Add(new XAttribute("No", newNo));
}
else
{
newNote.Attribute("No").Value = newNo.ToString();
}
Notes.AddFirst(newNote);
}
else
{
if (newNote.Attribute("No") == null)
{
newNote.Add(new XAttribute("No", 1));
}
else
{
newNote.Attribute("No").Value = "1";
}
Notes.AddFirst(newNote);
}
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw e;
//return false;
}
}
//删除记录(单一索引)
public bool DeletNote(string no = "", params string[] propertyValues)
{
try
{
bool okFlag = false;
if (propertyValues.Length > xmlProperties.Length)
{
return false;
}
else
{
if (no == "") //按属性值相等删除
{
for (int i = 0; i < propertyValues.Length; i++)
{
if (propertyValues[i] == "") continue;
if (Notes.Elements(noteName).Count() == 0) return false;//数据文件内容为空
var proNotes = Notes.Elements(noteName).Elements(xmlProperties[i]).Where(m => m.Value == propertyValues[i]);
foreach (var item in proNotes)
{
item.Parent.Remove();
okFlag = true;
}
}
}
else //按编号相等删除
{
if (Notes.Elements(noteName).Count() == 0) return false;//数据文件内容为空
var proNote = Notes.Elements(noteName).SingleOrDefault(m => m.Attribute("No").Value == no);
if (proNote != null)
{
proNote.Remove();
okFl |