C# 如何使用此结构中的linq to xml检索子节点并将其添加到选定类别中?
我想根据用户选择的类别将“联系人”添加到“联系人”中。假设用户从组合框中选择“朋友”,我将如何添加到所选类别中C# 如何使用此结构中的linq to xml检索子节点并将其添加到选定类别中?,c#,xml,linq,C#,Xml,Linq,我想根据用户选择的类别将“联系人”添加到“联系人”中。假设用户从组合框中选择“朋友”,我将如何添加到所选类别中 <PhoneContacts> <Categories> <Category Name="Colleagues"> <Contacts /> </Category> <Category Name="Friends"> <Contacts />
<PhoneContacts>
<Categories>
<Category Name="Colleagues">
<Contacts />
</Category>
<Category Name="Friends">
<Contacts />
</Category>
</Categories>
</PhoneContacts>
Fields to be added is this
<Contact>
<fullname>joe</fullname>
<phoneno>123456</phoneno>
<address>stack overflow</address>
</Contact>
要添加的字段如下所示
乔
123456
堆栈溢出
使用LINQ to XML很容易做到这一点。您需要创建一个新的XElement
,其中包含联系人信息,然后将其添加为相应类别中的Contacts
元素的子元素。为了简单起见,我建议使用一种方法来创建contactXElement
,然后简单地将其添加到XML中。它看起来像这样:
public XElement CreateContact(string name, string phone, string address)
{
XElement contact = new XElement("Contact",
new XElement("fullname", name),
new XElement("phoneno", phone),
new XElement("address", address));
return contact;
}
XDocument xDoc = XDocument.Load("contacts.xml");
string category = "Friends";
string name = "joe";
string phone = "123456";
string address = "stack overflow";
xDoc.Descendants("Contacts")
.Where(x => x.Parent.Attribute("Name").Value == category)
.Single()
.Add(CreateContact(name, phone, address));
xDoc.Save();
<PhoneContacts>
<Categories>
<Category Name="Colleagues">
<Contacts />
</Category>
<Category Name="Friends">
<Contact>
<fullname>joe</fullname>
<phoneno>123456</phoneno>
<address>stack overflow</address>
</Contact>
</Category>
</Categories>
</PhoneContacts>
然后您可以添加如下内容:
public XElement CreateContact(string name, string phone, string address)
{
XElement contact = new XElement("Contact",
new XElement("fullname", name),
new XElement("phoneno", phone),
new XElement("address", address));
return contact;
}
XDocument xDoc = XDocument.Load("contacts.xml");
string category = "Friends";
string name = "joe";
string phone = "123456";
string address = "stack overflow";
xDoc.Descendants("Contacts")
.Where(x => x.Parent.Attribute("Name").Value == category)
.Single()
.Add(CreateContact(name, phone, address));
xDoc.Save();
<PhoneContacts>
<Categories>
<Category Name="Colleagues">
<Contacts />
</Category>
<Category Name="Friends">
<Contact>
<fullname>joe</fullname>
<phoneno>123456</phoneno>
<address>stack overflow</address>
</Contact>
</Category>
</Categories>
</PhoneContacts>
上面的代码通过加载XML文件创建一个XDocument
(xDoc)
LINQ语句根据与类别变量匹配的父节点的名称
属性选择正确的联系人
节点(还请注意,它希望只有一个匹配项)。然后,它通过CreateContact
返回的XElement
添加一个新的Contact
节点组
然后保存更新后的XML文件
新的XML将如下所示:
public XElement CreateContact(string name, string phone, string address)
{
XElement contact = new XElement("Contact",
new XElement("fullname", name),
new XElement("phoneno", phone),
new XElement("address", address));
return contact;
}
XDocument xDoc = XDocument.Load("contacts.xml");
string category = "Friends";
string name = "joe";
string phone = "123456";
string address = "stack overflow";
xDoc.Descendants("Contacts")
.Where(x => x.Parent.Attribute("Name").Value == category)
.Single()
.Add(CreateContact(name, phone, address));
xDoc.Save();
<PhoneContacts>
<Categories>
<Category Name="Colleagues">
<Contacts />
</Category>
<Category Name="Friends">
<Contact>
<fullname>joe</fullname>
<phoneno>123456</phoneno>
<address>stack overflow</address>
</Contact>
</Category>
</Categories>
</PhoneContacts>
乔
123456
堆栈溢出
谢谢。让我试试看