Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Add more detail on creating an XML file

Add more detail on creating an XML file

Scheduled Pinned Locked Moved C#
xmljsonhelptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10603967
    wrote on last edited by
    #1

    I am able to create a XML file like this:

    testsuite

    Colony

    arc

    With the following code:

    // Create a new file in C:\\ dir
    XmlTextWriter textWriter = new XmlTextWriter("C:\\XML files\\API_PASS.xml", null);

    // Opens the document
    textWriter.WriteStartDocument();

    // Write first element
    textWriter.WriteStartElement("testsuite");

    // Write next element
    textWriter.WriteStartElement("Name", "");
    textWriter.WriteString("testsuite");
    textWriter.WriteEndElement();

    // Write one more element
    textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");
    textWriter.WriteEndElement();

    // WriteChars
    char[] ch = new char[3];
    ch[0] = 'a';
    ch[1] = 'r';
    ch[2] = 'c';
    textWriter.WriteStartElement("Char");
    textWriter.WriteChars(ch, 0, ch.Length);
    textWriter.WriteEndElement();

    // Ends the document.
    textWriter.WriteEndDocument();

    // close writer
    textWriter.Close();

    *) But I was required to add some more detail for the first element (keep the last line as it is), for example:

    -

    I tried to add in the:

    textWriter.WriteStartElement("testsuite errors=\"0\");

    but it shows nothing in the xml file Anyone can help?

    L S 2 Replies Last reply
    0
    • U User 10603967

      I am able to create a XML file like this:

      testsuite

      Colony

      arc

      With the following code:

      // Create a new file in C:\\ dir
      XmlTextWriter textWriter = new XmlTextWriter("C:\\XML files\\API_PASS.xml", null);

      // Opens the document
      textWriter.WriteStartDocument();

      // Write first element
      textWriter.WriteStartElement("testsuite");

      // Write next element
      textWriter.WriteStartElement("Name", "");
      textWriter.WriteString("testsuite");
      textWriter.WriteEndElement();

      // Write one more element
      textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");
      textWriter.WriteEndElement();

      // WriteChars
      char[] ch = new char[3];
      ch[0] = 'a';
      ch[1] = 'r';
      ch[2] = 'c';
      textWriter.WriteStartElement("Char");
      textWriter.WriteChars(ch, 0, ch.Length);
      textWriter.WriteEndElement();

      // Ends the document.
      textWriter.WriteEndDocument();

      // close writer
      textWriter.Close();

      *) But I was required to add some more detail for the first element (keep the last line as it is), for example:

      -

      I tried to add in the:

      textWriter.WriteStartElement("testsuite errors=\"0\");

      but it shows nothing in the xml file Anyone can help?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You don't need to write another element, but an attribute[^] inside that element.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • U User 10603967

        I am able to create a XML file like this:

        testsuite

        Colony

        arc

        With the following code:

        // Create a new file in C:\\ dir
        XmlTextWriter textWriter = new XmlTextWriter("C:\\XML files\\API_PASS.xml", null);

        // Opens the document
        textWriter.WriteStartDocument();

        // Write first element
        textWriter.WriteStartElement("testsuite");

        // Write next element
        textWriter.WriteStartElement("Name", "");
        textWriter.WriteString("testsuite");
        textWriter.WriteEndElement();

        // Write one more element
        textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");
        textWriter.WriteEndElement();

        // WriteChars
        char[] ch = new char[3];
        ch[0] = 'a';
        ch[1] = 'r';
        ch[2] = 'c';
        textWriter.WriteStartElement("Char");
        textWriter.WriteChars(ch, 0, ch.Length);
        textWriter.WriteEndElement();

        // Ends the document.
        textWriter.WriteEndDocument();

        // close writer
        textWriter.Close();

        *) But I was required to add some more detail for the first element (keep the last line as it is), for example:

        -

        I tried to add in the:

        textWriter.WriteStartElement("testsuite errors=\"0\");

        but it shows nothing in the xml file Anyone can help?

        S Offline
        S Offline
        Senthilkumar C K
        wrote on last edited by
        #3

        code snippet for adding attribute in xml:

        xmlWriter.WriteStartElement("testsuite");
        xmlWriter.WriteAttributeString("errors", "0");

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups