问题
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.
回答1:
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");
回答2:
The error means that there is already a variable e
declared outside the lambda expression.
回答3:
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.
来源:https://stackoverflow.com/questions/18527262/c-sharp-xml-remove-more-lines-at-once-but-attribute-values-can-change