title: Easiest way to add xml a node with a bunch of children nodes in .Net?
body: <p>I need to modify a xml file (actually a .rdlc report file) and add some nodes which have a lot of children nodes (and those children nodes again have children nodes). Actually they are almost the same strucutre, like this one:</p>
  
  <pre><code>           &lt;TablixRow&gt;
              &lt;Height&gt;0.23622in&lt;/Height&gt;
              &lt;TablixCells&gt;
                &lt;TablixCell&gt;
                  &lt;CellContents&gt;
                    &lt;Textbox Name="Textbox1"&gt;
                      &lt;CanGrow&gt;true&lt;/CanGrow&gt;
                      &lt;KeepTogether&gt;true&lt;/KeepTogether&gt;
                      &lt;Paragraphs&gt;
                        &lt;Paragraph&gt;
                          &lt;TextRuns&gt;
                            &lt;TextRun&gt;
                              &lt;Value/&gt;
                              &lt;Style/&gt;
                            &lt;/TextRun&gt;
                          &lt;/TextRuns&gt;
                          &lt;Style/&gt;
                        &lt;/Paragraph&gt;
                      &lt;/Paragraphs&gt;
                      &lt;rd:DefaultName&gt;Textbox1&lt;/rd:DefaultName&gt;
                      &lt;Style&gt;
                        &lt;Border&gt;
                          &lt;Style&gt;None&lt;/Style&gt;
                        &lt;/Border&gt;
                        &lt;PaddingLeft&gt;2pt&lt;/PaddingLeft&gt;
                        &lt;PaddingRight&gt;2pt&lt;/PaddingRight&gt;
                        &lt;PaddingTop&gt;2pt&lt;/PaddingTop&gt;
                        &lt;PaddingBottom&gt;2pt&lt;/PaddingBottom&gt;
                      &lt;/Style&gt;
                    &lt;/Textbox&gt;
                  &lt;/CellContents&gt;
                &lt;/TablixCell&gt;
                &lt;TablixCell&gt;
                  &lt;CellContents&gt;
                    &lt;Textbox Name="Textbox5"&gt;
                      &lt;CanGrow&gt;true&lt;/CanGrow&gt;
                      &lt;KeepTogether&gt;true&lt;/KeepTogether&gt;
                      &lt;Paragraphs&gt;
                        &lt;Paragraph&gt;
                          &lt;TextRuns&gt;
                            &lt;TextRun&gt;
                              &lt;Value/&gt;
                              &lt;Style/&gt;
                            &lt;/TextRun&gt;
                          &lt;/TextRuns&gt;
                          &lt;Style/&gt;
                        &lt;/Paragraph&gt;
                      &lt;/Paragraphs&gt;
                      &lt;rd:DefaultName&gt;Textbox5&lt;/rd:DefaultName&gt;
                      &lt;Style&gt;
                        &lt;Border&gt;
                          &lt;Style&gt;None&lt;/Style&gt;
                        &lt;/Border&gt;
                        &lt;PaddingLeft&gt;2pt&lt;/PaddingLeft&gt;
                        &lt;PaddingRight&gt;2pt&lt;/PaddingRight&gt;
                        &lt;PaddingTop&gt;2pt&lt;/PaddingTop&gt;
                        &lt;PaddingBottom&gt;2pt&lt;/PaddingBottom&gt;
                      &lt;/Style&gt;
                    &lt;/Textbox&gt;
                  &lt;/CellContents&gt;
                &lt;/TablixCell&gt;
              &lt;/TablixCells&gt;
            &lt;/TablixRow&gt;
  </code></pre>
  
  <p>So what is the easiest way to do it? In normal case I just create a XmlNode and some XmlAttribute objects, attach those attributes to the node, and create the children nodes the same way and finally append each child node into its parent. Needless to say it will be tedious to process my example node. Is there an easier way to do it? Like with the class XmlDocument, there is a function LoadXml (xml as string), which takes a string as the whole xml file and construct the strucutre. Is there a similar way to construct a XmlNode object? So that I only need to provide the whole piece of string representing my node, and then navigate to the child node of which I need to change the value. Thanks!</p>
  
  <p>Update:
  I am using VB.NET. And there is one problem with namespace when using XElement. In this link
  <a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx#Y1999" rel="nofollow">XName Class</a>, it says for C# it is recommended to just use overriden add operator to combine element and NS, but for VB, it recommends using Import  at the top (in the example outside of a Module. I assume it should also work for Class)and then everything will be using this NS automatically. However, this is not the case. For example, if I give </p>
  
  <pre><code>             Dim para As XElement = _
                  &lt;ReportParameter Name="HasErr"&gt;
                      &lt;DataType&gt;Boolean&lt;/DataType&gt;
                      &lt;DefaultValue&gt;
                          &lt;Values&gt;
                              &lt;Value&gt;False&lt;/Value&gt;
                          &lt;/Values&gt;
                      &lt;/DefaultValue&gt;
                      &lt;Prompt&gt;ReportParameter1&lt;/Prompt&gt;
                  &lt;/ReportParameter&gt;
  </code></pre>
  
  <p>it will automatically attach my specified (and stated as default) NS there. But if I use XElement.Parse(xml As String), where xml is the same string representing the xml, it won't add this NS at all, which will end up using an empty NS (the reason I want to use XElement.Parse is I want to give my customized parameter value there, like  &amp; MY_TYPE_NAME &amp; ).
  The second problem is when using @JohnD 's code, I try </p>
  
  <pre><code>xdoc.Root.Elements("ReportParameters").FirstOrDefault()
  </code></pre>
  
  <p>which I assume will also use my declared and default NS, will return nothing, i.e., it searches within empty namespace, but it is actually in the NS I mentioned. </p>
  
  <p>Anyone knows that is the reason MS made it so that there is no constructor for XName class, where I can specify a namespace before I use it? It says there is only one implicit convert, so when given a string representing the element name in </p>
  
  <pre><code>xdoc.Root.Elements("ReportParameters")
  </code></pre>
  
  <p>it will implicitly generates one XName object to index the search in Elements. But it is really clumsy. </p>
  
  <p>Latest update:
  I now found the solution to solve my first problem in my update: I am now using XML Literals to create XElement, and it is possible to use expression inside it. So it now looks like this:</p>
  
  <pre><code> Dim paraDefNode As XElement = _
                  &lt;ReportParameter Name=&lt;%= para.Value %&gt;&gt;
                      &lt;DataType&gt;String&lt;/DataType&gt;
                      &lt;DefaultValue&gt;
                          &lt;Values&gt;
                              &lt;Value&gt;False&lt;/Value&gt;
                          &lt;/Values&gt;
                      &lt;/DefaultValue&gt;
                      &lt;Prompt&gt;ReportParameter1&lt;/Prompt&gt;
                  &lt;/ReportParameter&gt;
  </code></pre>
  
  <p>and it will add my specified NS. (like I said, XElement.Parse(string) won't add it) So now I can construct the correct node. For my second problem I still can't figure out: I can't navigate to the target node by using the element name since it won't search for correct NS.</p>
  
  <p>I will anyway mark @JohnD 's post as answer since he suggested to use LINQ to XML.</p>