XML file creation using XDocument in C#

XML file creation using XDocument in C#

Asked on January 8, 2019 in XML.
Add Comment


  • 5 Answer(s)

    The LINQ to XML lets this to be more simpler, over three features:

    • User can construct an object without knowing the document it is a part of
    • They can construct an object and afford the children as arguments
    • If an argument is iterable, it will be iterated over

    So do the below code,

    void Main()
    {
      List<string> list = new List<string>
      {
         "Data1", "Data2", "Data3"
      };
      XDocument doc =
        new XDocument(
          new XElement("file",
            new XElement("name", new XAttribute("filename", "sample")),
            new XElement("date", new XAttribute("modified", DateTime.Now)),
            new XElement("info",
              list.Select(x => new XElement("data", new XAttribute("value", x)))
            )
          )
        );
     
      doc.Save("Sample.xml");
    }
    

         By using this code layout determindly to make the code itself to reflect the structure of the document.

    If the user need an element which consist a text node, they can construct that by passing in the text as another constructor argument:

    // Constructs <element>text within element</element>
    XElement element = new XElement("element", "text within element");
    
    Answered on January 8, 2019.
    Add Comment
    <file>
       <name filename="sample"/>
       <date modified ="  "/>
       <info>
         <data value="Data1"/> 
         <data value="Data2"/>
         <data value="Data3"/>
       </info>
    </file>

     

    Answered on February 28, 2019.
    Add Comment
    List<string> lst;
    XmlDocument XD = new XmlDocument();
    XmlElement root = XD.CreateElement("file");
    XmlElement nm = XD.CreateElement("name");
    nm.SetAttribute("filename", "Sample");
    root.AppendChild(nm);
    XmlElement date = XD.CreateElement("date");
    date.SetAttribute("modified", DateTime.Now.ToString());
    root.AppendChild(date);
    XmlElement info = XD.CreateElement("info");
    for (int i = 0; i < lst.Count; i++) 
    {
        XmlElement da = XD.CreateElement("data");
        da.SetAttribute("value",lst[i]);
        info.AppendChild(da);
    }
    root.AppendChild(info);
    XD.AppendChild(root);
    XD.Save("Sample.xml");
    Answered on February 28, 2019.
    Add Comment
    void Main()
    {
        List<string> list = new List<string>
        {
            "Data1", "Data2", "Data3"
        };
    
        XDocument doc =
          new XDocument(
            new XElement("file",
              new XElement("name", new XAttribute("filename", "sample")),
              new XElement("date", new XAttribute("modified", DateTime.Now)),
              new XElement("info",
                list.Select(x => new XElement("data", new XAttribute("value", x)))
              )
            )
          );
    
        doc.Save("Sample.xml");
    }

     

    Answered on February 28, 2019.
    Add Comment

    LINQ to XML allows this to be much simpler, through three features:

    • You can construct an object without knowing the document it’s part of
    • You can construct an object and provide the children as arguments
    • If an argument is iterable, it will be iterated over
    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.