gpt4 book ai didi

c# - 将记录追加到 Xml ASP.Net MVC5

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:28 27 4
gpt4 key购买 nike

根据 Controller 中的索引,我如何将记录附加到 XML 文件?我做了一些研究,但我似乎无法全神贯注。

索引( Controller )

public ActionResult Index(string sortOrder)
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml");
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record").Cast<XmlNode>().Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});

switch (sortOrder)
{
case "ID":
persons = persons.OrderBy(Personal => Personal.ID);
break;

case "Name":
persons = persons.OrderBy(Personal => Personal.Name);
break;

case "City":
persons = persons.OrderBy(Personal => Personal.City);
break;

default:
break;
}

return View(persons.ToList());
}

我尝试过的:

创建( Controller )

    [HttpPost]
public ActionResult Create(FormCollection collection)
{
string xmlFile = "C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record")
.Cast<XmlNode>()
.Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
persons.appendTo(xmlFile);
return RedirectToAction("Index");
}
catch
{
return View();
}
}

我不擅长语法,所以这可能都是错误的。

最佳答案

对于 C# 中可用的一些类的初学者,起诉您正在使用的特定类,您应该能够执行以下操作

XDocument doc = XDocument.Load(xmlFile);
var parent = doc.Descendants("NameOfParentTag").FirstOrDefault();//if there is only one
parent.Add(new XElement("personal",
new XElement("ID", ID_Value),
new XElement("Name" = Name_Value),
new XElement("Email", Email_value)));
doc.Save(xmlFile);

这会将值附加到您的 xmlFile,NameOfParentTag 是您要插入值的 parentLocation 的名称,在您的情况下,我假设这将是 record

第二种方式是

doc.Load(xmlFile);
XmlNode editNode = doc.SelectSingleNode("targetNode");
XmlNode node = nodes[0];//get the specific node, not sure about your xml structure
XmlElement elem = node.OwnerDocument.CreateElement("personal");
elem.InnerXml = personal.SerializeAsXml();
node.AppendChild(elem);

SerializeAsXml 方法的样子

public static string SerializeAsXml(this Personal personal)
{
var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

//serialize the binding
string xmlOutput = string.Empty;
using (StringWriter stream = new StringWriter())
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
XmlSerializer serializer = new XmlSerializer(personal.GetType());
serializer.Serialize(xmlWriter, obj, emptyNamepsaces);
xmlOutput = stream.ToString();
}

return xmlOutput;
}

注意,对于上述方法,您需要使用必要的 attributes 修饰您的 XML

关于c# - 将记录追加到 Xml ASP.Net MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com