代码之家  ›  专栏  ›  技术社区  ›  Christopher B. Adkins

用c语言创建XML#

  •  2
  • Christopher B. Adkins  · 技术社区  · 16 年前

    首先,这是我的代码。

    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 
                                                 + @"' />"));
    }
    

    更新-XML编写器的代码

    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))
    

    从这里开始,它不会向上述代码中包含的元素添加新行。

    1 回复  |  直到 16 年前
        1
  •  2
  •   Jeff Yates    16 年前

    我将按如下方式编写循环,提供名称空间。这将根据需要创建节点。

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

    我在这里假定 wixNS 是一个例子 XNamespace

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