0

as i said in the Title i want to delete the node when i get a specific value out of an attribute. (in this case "install" out of the attribute "dependencyType")

Xml File:

<dependency>
<dependentAssembly dependencyType="preRequisite">
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install">
</dependentAssembly>
</dependency>

How it shall look like(after i ran through):

<dependency>
<dependentAssembly dependencyType="preRequisite">
</dependentAssembly>
</dependency>

i want to remove the node (in this case "dependency" when i get in the attribute "dependencytype" the value "install").

i tried two things 1st Code:

private void DeleteXmlPopulates()
    {
        string filepath = "C:\Example\Example.exe.manifest";
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);
        using (XmlReader reader = XmlReader.Create(filePath))
        {
            while (reader.Read())
            {
                if (reader.Name == "dependency")
                {
                    if (reader.GetAttribute("dependencyType") == "install")
                    {
                       //here i shall get the current Node-name and .Remove(); it
                    }
                }
            }
        }

2nd Code:

        private void DeleteXmlPopulates()
    {
        string filepath = "C:\Example\Example.exe.manifest";
        XmlDocument doc = new XmlDocument();
        doc.Load(filePath);
        var xml = XElement.Load(File.OpenRead(filePath));

        var elementsToDelete = xml.Descendants().Where(x => x.Name == "dependentAssembly" && x.Attribute("dependencyType") != null && x.Attribute("dependencyType").Value == "install");

        foreach (var xElement in elementsToDelete)
        {
            xElement.Remove();
        }
        xml.Save(filePath);
    }

The problem is in the first one is given in the code

The problem is in the second one is that if i get "elementsToDelete" for the foreach-loop and it comes back there arent any "elementsToDelete" so it skips the for each, after all of that, the programm tries to save (xml.save(filepath);) but it gets an exception that its already in use.

What shall i do? Why is it like that?

constructive criticism is Welcome :) correct everytihng you want in my question :) I hope its clear :)

2
  • What is the question? You get IOException ? You need to close the FileStream returned by File.OpenRead Commented Aug 5, 2014 at 6:43
  • Yes i get a System.IO.IOException it sais the File is already in use but if i look up if its in use it sais no Commented Aug 5, 2014 at 6:47

1 Answer 1

2

This is the problem in the second code:

XmlDocument doc = new XmlDocument();
doc.Load(filePath);
var xml = XElement.Load(File.OpenRead(filePath));

For some reason, you're loading the XML document twice in this (and then ignoring doc) - but you're leaving a stream open (the stream returned by File.OpenRead). Change this code to just:

var xml = XElement.Load(filePath);

and it should be fine. You can simplify the rest of the code using the Remove extension method though:

xml.Descendants()
   .Where(x => x.Name == "dependentAssembly" && 
               (string) x.Attribute("dependencyType") == "install")
   .Remove();

Note that casting XAttribute to string just returns null if the attribute doesn't exist (i.e. if you call it with a null reference).

So your who method becomes:

private void DeleteXmlPopulates()
{
    string filepath = "C:\Example\Example.exe.manifest";
    var xml = XElement.Load(filePath);
    xml.Descendants()
       .Where(x => x.Name == "dependentAssembly" && 
                   (string) x.Attribute("dependencyType") == "install")
       .Remove();
    xml.Save(filePath);
}
Sign up to request clarification or add additional context in comments.

6 Comments

you did a good job but if i use this code on the xml-file it can not be opened :/
@Gentle: What do you mean by that? Opened by what? I can't help you without more information.
if i go than in the explorer and try to open this file ("c:\exaple\example.exe.manifest") it throws an exception where it sais : "C:\example\Exaple.exe.manifest connot be opened"
@Gentle: Well have you tried opening it with notepad, just to see what's in there? It's entirely possible that by removing the element, you've made it invalid - I don't know enough details of manifest files to know. I suggest you change the code to save the modified file to a different filename, so you can then compare the two.
In the Code nothing happened. No Problem i'll look for a solution :) Have a great day
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.