WPF在Linq中使用Xml文件 [英] Wpf using Xml file with Linq

查看:76
本文介绍了WPF在Linq中使用Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这个xml文件Element< common name ="MAN ..">要使用Linq C#将位置更改为最后一个元素,我有这个XML

Hi , i want this xml file Element <common name="MAN.."> to change position as last Element using Linq C#, i have this XML

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <Local Name="Require" >
     </Local>
  <Host Name="Stills" >
     </Host>
    <Common Name="MAN"  ID="188b655f-fc47-43af-acd7-99d0764cca33">
                <Common Name="System" ID="188b655f-fc47-43af-acd7-99d0764cca3">
                  </Common>
                <Common Name="System"  ID="080f8b14-22a4-46d9-ac3f-57e6fcc87e04">
                  </Common>
    </Common>
</ROOT>




我终于要这样了




i want like this finally

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <Local Name="Require" >
     </Local>
  <Host Name="Stills" >
     </Host>
                <Common Name="System" ID="188b655f-fc47-43af-acd7-99d0764cca3">
                  </Common>
                <Common Name="System"  ID="080f8b14-22a4-46d9-ac3f-57e6fcc87e04">
                  </Common>

                   <Common Name="MAN"  ID="188b655f-fc47-43af-acd7-99d0764cca33">
                   </Common>
</ROOT>

推荐答案

由于我没有正确阅读您的XML,所以请不要修改我的最后一个解决方案.您正在修改树的层次结构,因此可以使用Decentant进行查找.找到它们之后,您可以将它们放在重复的XDocument中.
之后,您可以将它们是"的节点减去已移动的节点.

在下面,我将其分解为2种方法.一种读取数据,另一种处理您的重新排序.

Disreguard my last solution as I did not read your XML properly. You are modifying the heirarchy of the tree, so you can use Decentants to seek it out. After seeking them you can place them in a duplicate XDocument.
After that you can put in the nodes they ''were'' in minus the nodes that have been moved.

Bellow I have broken it down into 2 methods. One that reads the data in, and one that handles your reordering.

private void ReadXML()
{
    _rawXML = XDocument.Load(PATH_INPUT);
            
    //fill our duplicate
    _sortedXML = XDocument.Load(PATH_INPUT);

    //Remove the data we will sort on so it can be repopulated
    var nodesThatWillBeRepopulated = _sortedXML.Root.Elements(ELEMENT_SORT).ToList();
    foreach (var node in nodesThatWillBeRepopulated)
    {
        node.Remove();
    }
}



我们要做的第一件事是收集所有要移动的节点的汇编(在元素类型"Common"的"Name"字段中使用"System".然后,我们还抓取那些不被移动但具有'从其节点中删除的已移动"对象.

循环完成实际工作.



The first thing we do is get a compilation of all the nodes to be moved (with "System" in the "Name" field of element type "Common". Then we also grab the ones that are to not be moved but have the ''moved'' ones removed from their node.

The loops do the actual work.

private void SortXML()
{
    var moveThese = _rawXML.Root.Descendants(ELEMENT_SORT).Where(node => node.Attribute(ATTRIBUTE_COMP) != null &&  string.Equals(node.Attribute(ATTRIBUTE_COMP).Value, ATTRIBUTE_MOVE, StringComparison.CurrentCultureIgnoreCase));
    var addTheseBackInLast = _rawXML.Root.Descendants(ELEMENT_SORT).Where(node => node.Attribute(ATTRIBUTE_COMP) != null && string.Equals(node.Attribute(ATTRIBUTE_COMP).Value, ATTRBITE_SEEK, StringComparison.CurrentCultureIgnoreCase));


    //Now add copied nodes to the 'duplicate' document at the desired position in the tree (one bellow the root)
    foreach (var node in moveThese)
    {
        _sortedXML.Root.Add(new XElement(node));
    }

    //Now add the node with out the moved contents
    foreach (var node in addTheseBackInLast)
    {
        //First remove the data
        var removeThese = node.Descendants(ELEMENT_SORT).Where(element => element.Attribute(ATTRIBUTE_COMP) != null &&  string.Equals(element.Attribute(ATTRIBUTE_COMP).Value, ATTRIBUTE_MOVE, StringComparison.CurrentCultureIgnoreCase)).ToList();
        foreach (var removeThis in removeThese)
        {
            removeThis.Remove();
        }

        //Now add it in
        _sortedXML.Root.Add(new XElement(node));
    }

    //_sortedXML is now in the form you need, so save send or whatever
}




这是我使用的常量(硬编码到您的XML数据)




And here are the constants I was using (hardcoded to your XML data)

private const string ATTRIBUTE_MOVE = "System";
private const string ELEMENT_SORT = "Common";
private const string ATTRBITE_SEEK = "Man";
private const string ATTRIBUTE_COMP = "Name";
private XDocument _rawXML;
private XDocument _sortedXML;



让我知道您是否不了解某些内容.



Let me know if you do not understand something.


不确定为什么标记了此WPF(这只是XML和LINQ)

您可以使用 XDocument [
完成后,您可以查询出要更改顺序的所有元素.
var reorderThese = xDoc.Elements("Common")

现在,您已经拥有了对元素进行排序的元素.我建议隐含
IComparer [
然后,您只需要调用sort方法并使用IComparer< T>的实现即可.
reorderThese.Sort(new CustomerSorter())

现在,您已经对元素进行了排序,只需将它们放入重复的xDoc中即可.
duplicate.Root.Add(reorderThese).现在,您想要的是重复" XDocument,因此您可以用该数据覆盖原始文件.

看来您实际上是在改变层次结构.让我考虑一下.
Not sure why you tagged this WPF (this is just XML and LINQ)

You can use the XDocument[^] to read the data in. Then create a ''duplicate'' with out the elements you plan to move (get the root ''local'' and ''host'' elements as you have them).

After you have done that you can query out all of the elements you want to change the order on.
var reorderThese = xDoc.Elements("Common")

Now that you have the elements you just need to sort them. I would recomend implimenting the IComparer[^] interface and use that comparitor. I can only assume you want the one element because it is a "Man".. Maybe you want to swap because it is the first index (that is a bit odd though, seems somewhat arbitrary then). By building a sorter you will handle the needs if sorting becomes more complicated etc.

You then need only call the sort method and use your implimentation of IComparer<T>.
reorderThese.Sort(new CustomerSorter())

Now that you have the elements sorted just put them in your duplicate xDoc.
duplicate.Root.Add(reorderThese). Now your ''duplciate'' XDocument is what you want so you can overwrite the original with that data.

Oh looks like you are actually chaning the heirarchy. Let me ponder.


这篇关于WPF在Linq中使用Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆