2

First off here is my code.

IEnumerable<XElement> targetDirectory =
    from XElement e in workingXmlDocument.Descendants(wixNS + "Directory")
    where e.Attribute("Id").Value == "TARGETDIR"
    select e;
foreach (var now in targetDirectory)
{
    now.Add(XElement.Parse("<Directory Id='" + fileVariable.Directory.Name 
                                             + @"' />"));
}

Here is what I am trying to do. I am trying to search for every Directory element with the attribute Id valued at TARGETDIR. Then I place a new directory element inside that one with a name of a file's directory. It does just that. The problem is that it just puts all the directories into a single line (no line breaks, no indent, nothing, just the raw data), and it includes a blank xmlns tag with every element. How do I tell it that each element should have it's own line in the XML document and how do I tell it to use the same namespace as the rest of the document? I know I could just explicitly tell it that it should have a xmlns attribute with the correct NS, but that is the last thing I want to do. ideas?

Update - the code for the XML Writer

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineHandling = NewLineHandling.Entitize;

using (XmlWriter currentWriter = XmlWriter.Create(filePath, settings))
{
     workingXmlDocument.WriteTo(currentWriter);
     currentWriter.Flush();
} // using (XmlWriter xw = XmlWriter.Create(FilePath))

From here it is not adding new lines to the included elements from the above code.

4
  • (I've reformatted to make it clear that the foreach loop isn't part of the query.) Commented Jan 27, 2010 at 14:36
  • Why is that the last thing you want to do? That's kinda how XML works. Commented Jan 27, 2010 at 14:37
  • @Jeff Yates - normally when I am working with WiX and XML in general if I only have one namespace in a file there is no need to have the xmlns attribute on every single element that is in it. Normally it is implied or assumed. I am trying to achieve that same end here. I am going to be creating an entire XML file by the time this program is done and I would rather not have the xmlns attribute for absolutely every element in it. Commented Jan 27, 2010 at 14:39
  • Oh, I understand. Yeah, you don't want to explicitly provide the xmlns attribute. Just add the XNamespace as you already did in your query. See my answer below. Commented Jan 27, 2010 at 14:40

1 Answer 1

2

I would write the loop as follows, providing the namespace. This should create the nodes as you want them.

foreach (var now in targetDirectory)
{
    now.Add(new XElement(
        wixNS + "Directory",
        new XAttribute("Id", fileVariable.Directory.Name));
}

I am presuming here that wixNS is an instance of XNamespace, such as:

XNamespace wixNS = "http://schemas.microsoft.com/wix/2003/01/wi";

I am not sure why the indenting is not working.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. That fixed the namespace issue. But how do I improve the formatting of the output. All elements created in this loop are just placed on one line
@Update - I already have that included with my writer. I will add the code for my writer to the original post.
@Adkins: Not sure why that doesn't work. Perhaps ask new question so you can get some others to weigh in on this particular problem.

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.