C#XML一次删除多个行,但属性值可以改变 [英] C# XML remove more lines at once, but attribute values can change

查看:230
本文介绍了C#XML一次删除多个行,但属性值可以改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须写在C#中(Windows窗体)代码加载一个XML文件到一个RichTextBox。工作原理

We have to write a code in C# (windows form) that loads a XML file into a richtextbox. WORKS

这是一个文本框的样子:

This is what a textfield looks like:

<Hmi.Screen.TextField Name="Text Field_1" AggregationName="ScreenItems" ID="31">
                        <ObjectList>
                          <Hmi.Screen.Property Name="Layer" AggregationName="Properties" ID="77">
                            <AttributeList>
                              <Value>0</Value>
                            </AttributeList>
                          </Hmi.Screen.Property>
                          <Hmi.Screen.Property Name="Left" AggregationName="Properties" ID="78">
                            <AttributeList>
                              <Value>264</Value>
                            </AttributeList>
                          </Hmi.Screen.Property>
                          <Hmi.Screen.Property Name="Top" AggregationName="Properties" ID="79">
                            <AttributeList>
                              <Value>48</Value>
                            </AttributeList>
                          </Hmi.Screen.Property>
                          <Hmi.Screen.Property Name="FitToLargest" AggregationName="Properties" ID="84">
                            <AttributeList>
                              <Value>false</Value>
                            </AttributeList>
                          </Hmi.Screen.Property>
                        </ObjectList>
                      </Hmi.Screen.TextField>

这是我们要删除或设置由值它的一部分假真正(你的选择是什么容易):

This is the part of it we want to delete OR set the value from false to true (your choice what's easier):

<Hmi.Screen.Property Name="FitToLargest" AggregationName="Properties" ID="84">
  <AttributeList>
    <Value>false</Value>
  </AttributeList>
</Hmi.Screen.Property>



的码本部分中的每个文本字段中发现的,但与用于属性ID不同的值。我们想删除它为每一个文本框

This part of code is found in every textfield, but with a different value for the attribute ID. We want to delete it for every textfield.

我们已经有了这样的:

XDocument xml = XDocument.Load(loadLocation);
        xml.Descendants().Elements("Hmi.Screen.Property")
                         .Where(e => e.Attribute("Name=").Value == "FitToLargest")
                         .Remove();
        xml.Save(loadLocation);

我们发现它计算器,但它给这个错误:

We found it on stackoverflow, but it gives this error:

Error   1   A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else C:\Users\*****\*****\*****\Projects\Converter\Converter\Form1.cs    211 37  Converter

我们可以通过改变电子来,例如 eJbou (Jbou是我名字的缩写)

We can remove the error by changing the e to for example eJbou (Jbou are my initials)

我们希望有人能告诉帮助我们什么错误意味着,或者帮助我们放弃,做的工作守则。

We hope somebody can help us by telling what the error means, or help us by give a code that does work.

推荐答案

我希望你做这个的Page_Load
以便检查这种

I hope you are doing this on Page_Load so check this

protected void Page_Load(object sender, EventArgs e)
    {

电子包含事件数据
所以,如果你正在使用它的任何事件中,那么你必须用别的东西像 X要更换你的电子 说,

e contains the event data So if you are using it inside any event then you have to replace your e with something else like x say ,

XDocument xml = XDocument.Load(loadLocation);
        xml.Descendants().Elements("Hmi.Screen.Property")
                         .Where(x=> x.Attribute("Name").Value == "FitToLargest")
                         .Remove();
        xml.Save(loadLocation);



试试这个

try this

   XDocument delete = XDocument.Load(@"D:\xxx\Delete.xml");
               delete.Descendants("Hmi.Screen.Property").Where(
             x => (string)x.Attribute("Name") == "FitToLargest").Remove();
            //e => e.Attribute("Name").Value == "FitToLargest").Remove();
            delete.Save(@"D:\xxxt\xxxx\Delete.xml");

这篇关于C#XML一次删除多个行,但属性值可以改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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