English 中文(简体)
Update XML file with Linq
原标题:Update XML file with Linq

我在努力更新我的xml档案,使其具有新的价值。 我有一个类别<代码>Person,其中仅包括2个插图、名称和说明。 我把这份名单编成XML文件。 然后,我罗列一份新名单,其中载有许多相同的名字,但其中一些名单载有其他名单没有载列的描述。 我怎么能够核实,目前的XML档案中的名称是否包含“无描述”以外的价值,而“无”是没有的?

零件:

<?xml version="1.0" encoding="utf-8"?>
<Names>
  <Person ID="2">
    <Name>Aaron</Name>
    <Description>No description</Description>
  </Person>
  <Person ID="2">
    <Name>Abdi</Name>
    <Description>No description</Description>
  </Person>
</Names>

And this is the method for writing the list to the xml file:

public static void SaveAllNames(List<Person> names)
{
    XDocument data = XDocument.Load(@"xmloysNames.xml");   

    foreach (Person person in names)
    {
        XElement newPerson = new XElement("Person",
                                 new XElement("Name", person.Name),
                                 new XElement("Description", person.Description)
                             );

        newPerson.SetAttributeValue("ID", GetNextAvailableID());

        data.Element("Names").Add(newPerson);
    }
    data.Save(@"xmloysNames.xml");
}

在前 lo中,我是如何检查一个人的名字是否已经到来的,然后检查描述是否是“无描述”以外的东西,如果是的话,用新的信息加以更新?

最佳回答

我不相信我能正确理解你想要的东西,但我假定,你只想在名字已经到时更新描述,说明目前是<代码>。 无说明<>。 (你可能改为空洞,BTW)。

You could put all the Persons into a Dictionary based by name:

var doc = …;

var persons = doc.Root.Elements()
                      .ToDictionary(x => (string)x.Element("Name"), x => x);

并询问:

if (persons.ContainsKey(name))
{
    var description = persons[name].Element("Description");
    if (description.Value == "No description")
        description.Value = newDescription;
}

也就是说,如果你关心业绩的话。 如果你不,你就不需要字典:

var person = doc.Root.Elements("Person")
                     .SingleOrDefault(x => (string)x.Element("Name") == name);

if (person != null)
{
    var description = person.Element("Description");
    if (description.Value == "No description")
        description.Value = newDescription;
}
问题回答

您可使用: Nodes-Method on XElement and questionnaires。

但我会建议你使用

For XPath expression take a look at: How to check if an element exists in the xml using xpath?

I think you could create a peoplelist which only contains people not in the xml.

like

        var containlist = (from p in data.Descendants("Name") select p.Value).ToList();
        var result = (from p in peoplelist where !containlist.Contains(p.Name) select p).ToList();

因此,你不需要改变你存在的方法......

只是之后。

SaveAllNames(result); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签