0

I need to change the second line to config schema ="xxx.xsd" how can I do that ?

1<?xml version="1.0" encoding="utf-8"?>
2<**config**>
3<maketool-config>
.
.
.
</maketool-config>
</config>

here is the code :

 XElement triggerRoot = new XElement("config",
                new XElement("maketool-config",
            );
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Aida E
  • 1,108
  • 2
  • 22
  • 40
  • duplicate of http://stackoverflow.com/questions/7931650/adding-elements-to-an-xml-file-in-c-sharp?rq=1 – Kaido Jan 20 '14 at 13:49
  • take a look at http://stackoverflow.com/questions/751511/validating-an-xml-against-referenced-xsd-in-c-sharp – Paul Zahra Jan 20 '14 at 13:50
  • I want to change the configurations and already checked those links – Aida E Jan 20 '14 at 14:08
  • 1
    Do you want to insert `schema="xxx.xsd"` or `xmlns="xxx.xsd"`? The first one would be equivalent to the insertion of an attribute. The second is alternation of a namespace, which is much more complex, because changing this line might (or might not, depending on your needs) change all other elements as well. – Thomas Weller Jan 20 '14 at 14:17
  • If you want to change an existing file, you should not create a new `XElement`. You should load the XML into a `XDocument`. – Thomas Weller Jan 20 '14 at 14:18
  • yes I want to change an existing file and thanks , I fixed that :) – Aida E Jan 20 '14 at 14:51

1 Answers1

0
        var doc = new XmlDocument();
        var triggerRoot = new XElement("config", new XElement("maketool-config", ""));
        var newAttribute = doc.CreateAttribute("schema");
        newAttribute.Value = "xxx.xsd";
        triggerRoot.SetAttributeValue("schema", "xxx.xsd");
charrcy
  • 51
  • 3
  • if you already loaded the document, simply call Attributes.Append(newAttribute) on the document root – charrcy Jan 20 '14 at 15:11