none
who we can write a xml document in c# RRS feed

  • Question

  • Hi I  have XML data , I have to write that data in my code with the help of c# how can i do that code with the help of condition .Please find the below data and how we can write that data .

    ?xml version="1.0" encoding="UTF-16"?>

    -<Records count="7">


    -<Metadata>


    -<FieldDefinitions>

    <FieldDefinition alias="Tracking_ID" name="Tracking ID" guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371"/>

    <FieldDefinition alias="Title" name="Title" guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374"/>

    <FieldDefinition alias="Description" name="Description" guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375"/>

    </FieldDefinitions>

    </Metadata>





    -<LevelCounts>

    <LevelCount count="7" guid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" id="4696"/>

    </LevelCounts>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222486">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222486</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222487">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222487</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">test</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1"><p> test</p></Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222490">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222490</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this takes soo less time</Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222491">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222491</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this takes soo less time</Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222492">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222492</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222493">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222493</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>

    </Record>


    -<Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222494">

    <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222494</Field>

    <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">dsgdfsgf</Field>

    <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1"><p> sdfgsdfgfdsg</p></Field>

    </Record>

    </Records> 
    Monday, February 12, 2018 7:49 AM

All replies

  • Hi Gourav Narula,

    Thank you for posting here.

    For your question, please try the code below.

     XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-16", ""),
                    new XElement("Records", new XAttribute("count", 7),
                       new XElement("Metadata",
                          new XElement("FieldDefinitions",
                              new XElement("FieldDefinition", new XAttribute("alias", "Tracking_ID"), new XAttribute("name", "Tracking ID"), new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371")),
                              new XElement("FieldDefinition", new XAttribute("alias", "Title"), new XAttribute("name", "Title"), new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374")),
                              new XElement("FieldDefinition", new XAttribute("alias", "Description"), new XAttribute("name", "Description"), new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"))
                                       )/*FieldDefinitions*/
                                   ),/*Metadata*/
                       new XElement("LevelCounts",
                           new XElement("LevelCount",
                              new XAttribute("count", 7), new XAttribute("guid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("id", "4696"))
                                   ),/* LevelCounts*/
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222486"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222486"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "ValueUpdateThroughApi"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"), "this is a update action")
                                   ),//Record
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222487"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222487"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "test"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"), "this is a update action",
                              new XElement("p", "test"))//Field
                                   ),//Record
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222490"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222490"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "ValueUpdateThroughApi"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"), "this takes soo less time")
                                   ),//Record
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222492"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222492"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "ValueUpdateThroughApi"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"), "this is a update action")
                                   ),//Record
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222493"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222493"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "ValueUpdateThroughApi"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"), "this is a update action")
                                   ),//Record
                       new XElement("Record",
                           new XAttribute("parentId", 0), new XAttribute("moduleId", "4887"), new XAttribute("levelGuid", "984facc4-3dbe-4bd7-b3e1-fcc370d06d1d"), new XAttribute("levelId", "4696"), new XAttribute("contentId", "222494"),
                           new XElement("Field", new XAttribute("guid", "2ba37d7a-e74f-49ad-9e20-aa2beb290695"), new XAttribute("id", "31371"), new XAttribute("type", "6"), "222494"),
                           new XElement("Field", new XAttribute("guid", "cf6183df-19c9-492a-b53a-cc4eb1dc8a16"), new XAttribute("id", "31374"), new XAttribute("type", "1"), "dsgdfsgf"),
                           new XElement("Field", new XAttribute("guid", "4220cccd-64f2-4db4-8ee6-6d2eb5a728c5"), new XAttribute("id", "31375"), new XAttribute("type", "1"),
                              new XElement("p", "sdfgsdfgfdsg"))//Field
                                   )//Record
                                 )//records
                              );//xdoc
                xDoc.Save("xDoc.xml");
    

    Here is the xml file I created.

    <?xml version="1.0" encoding="utf-16"?>
    <Records count="7">
      <Metadata>
        <FieldDefinitions>
          <FieldDefinition alias="Tracking_ID" name="Tracking ID" guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" />
          <FieldDefinition alias="Title" name="Title" guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" />
          <FieldDefinition alias="Description" name="Description" guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" />
        </FieldDefinitions>
      </Metadata>
      <LevelCounts>
        <LevelCount count="7" guid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" id="4696" />
      </LevelCounts>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222486">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222486</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>
      </Record>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222487">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222487</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">test</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action<p>test</p></Field>
      </Record>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222490">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222490</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this takes soo less time</Field>
      </Record>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222492">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222492</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>
      </Record>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222493">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222493</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">ValueUpdateThroughApi</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">this is a update action</Field>
      </Record>
      <Record parentId="0" moduleId="4887" levelGuid="984facc4-3dbe-4bd7-b3e1-fcc370d06d1d" levelId="4696" contentId="222494">
        <Field guid="2ba37d7a-e74f-49ad-9e20-aa2beb290695" id="31371" type="6">222494</Field>
        <Field guid="cf6183df-19c9-492a-b53a-cc4eb1dc8a16" id="31374" type="1">dsgdfsgf</Field>
        <Field guid="4220cccd-64f2-4db4-8ee6-6d2eb5a728c5" id="31375" type="1">
          <p>sdfgsdfgfdsg</p>
        </Field>
      </Record>
    </Records>

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 13, 2018 7:08 AM
    Moderator