0

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>

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.

We already have this:

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

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    possible duplicate of [Error A local variable cannot be declared in this scope](http://stackoverflow.com/questions/16263099/error-a-local-variable-cannot-be-declared-in-this-scope) – lc. Aug 30 '13 at 07:28
  • So name it anything else... Your initials are not such a good idea but `elt` or `hmi` would be OK. – H H Aug 30 '13 at 07:34
  • Oh okay. But does somebody have a code that does delete the part or change the false to true. – aRandomStudent Aug 30 '13 at 07:36
  • What's wrong with the `.Remove()` ? – H H Aug 30 '13 at 07:46
  • The syntax is right but it just doesn't work. Maybe it is the saving option? – aRandomStudent Aug 30 '13 at 07:54
  • 1
    @aRandomStudent Hope its a typo just check your query again `.Where(e => e.Attribute("Name=").Value` there is no attribute as `Name=` change it to `Name` . – Suraj Singh Aug 30 '13 at 08:42

3 Answers3

1

The error means that there is already a variable e declared outside the lambda expression.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
0

I hope you are doing this on Page_Load so check this

protected void Page_Load(object sender, EventArgs e)
    {

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");
Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • Thanks, the loading and saving works, but somehow it doesn't remove anything. – aRandomStudent Aug 30 '13 at 08:27
  • @aRandomStudent Well check one thing i am saving `Xdoc` to location not `delete` well check again i am updating code try now . – Suraj Singh Aug 30 '13 at 08:30
  • @aRandomStudent Howe-ever your code will also work just change the `e` with something else and something i saw in your code hope its a typing error , this part of your code `Where(e => e.Attribute("Name=").` there is no `attribute` as `Name=` change it to `Name` , Hope its just a typing error :-) – Suraj Singh Aug 30 '13 at 08:33
0

The problem I had was that it couldn't find the file when loading it. Be sure the file in in your bin -> debug. You can't just load any path somehow.