none
How to create XML document in C#.net

    Question

  • Hi All,

    I need to create xml document in c#.net,

    This is xml document

    <RoleHierarchyDetails xmlns="http://cts.com/rhms/RoleHierarchyEvent">
                <RoleHierarchyDetail>
                      <AssociateID></AssociateID>
                      <UserName>Sandeep Mutha</UserName>
                      <RoleId>R_ID_0018</RoleId>
                      <RoleName>SBU2 Head</RoleName>
                      <PortfolioName>AXA SWISS</PortfolioName>
                      <PortfolioType>SBU2</PortfolioType>
                      <PortfolioId>222</PortfolioId>
                      <HierarchyId>HT_ID_001</HierarchyId>
                      <HierarchyName>BU Hierarchy</HierarchyName>
                      <AlternatePortfolioId></AlternatePortfolioId>
                      <AlternateNodeName></AlternateNodeName>
                      <ActiveFlag>1</ActiveFlag>
                </RoleHierarchyDetail>
          </RoleHierarchyDetails>

     

    I need your help

    Thanks in Advance

     


    Balaswari
    • Moved by edhickey Wednesday, March 02, 2011 5:40 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Tuesday, March 01, 2011 4:56 AM

Answers

  • Hi you can create XML document using XML Serialization. Consider following C# method for converting object to XML string

    public string ToXML(Object oObject)
        {
          XmlDocument xmlDoc = new XmlDocument();
          XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
          using (MemoryStream xmlStream = new MemoryStream())
          {
            xmlSerializer.Serialize(xmlStream, oObject);
            xmlStream.Position = 0;
            xmlDoc.Load(xmlStream);
            return xmlDoc.InnerXml;
          }
        } 
    

    need to add following namespace for working with above method

    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    

    You can check following link

    *** http://www.c-sharpcorner.com/UploadFile/hasibul2363/5675/?ArticleID=fe08a5fa-2a34-4068-b11d-e66315e8d08e

    http://www.codeproject.com/KB/XML/GenericXmlSerialization.aspx

    http://msdn.microsoft.com/en-us/library/182eeyhh%28v=vs.71%29.aspx


    Hasibul Haque, MCTS http://blog.e-rains.com
    Thursday, March 03, 2011 10:37 AM
  • Hello Balaswari,

    Please check this sample: http://www.csharphelp.com/2006/05/creating-a-xml-document-with-c/.

    Also, here's a C# project on how to read/write xml files: http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Thursday, March 03, 2011 10:04 AM
  • Hi

    http://www.csharphelp.com/2006/05/creating-a-xml-document-with-c/

    http://www.devx.com/tips/Tip/21168

    http://www.codekicks.com/2007/04/reading-and-writing-xml-files-using-c.html

    I think these could help you. Feel free to get back if you still have problems.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula
    Thursday, March 03, 2011 10:39 AM
  • I'd do something like this - it will allow serialization and deserialization.
    using System;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication13
    {
      class Program
      {
        static void Main(string[] args)
        {
          RoleHierarchyDetail roleHierarchyDetail = new RoleHierarchyDetail()
          {
            AssociateId = "",
            UserName = "Sandeep Mutha",
            RoleId = "R_ID_0018",
            RoleName = "SBU2 Head",
            PortfolioName = "AXA SWISS",
            PortfolioType = "SBU2",
            PortfolioId = "222",
            HierarchyId = "HT_ID_001",
            HierarchyName = "BU Hierarchy",
            AlternatePortfolioId = "",
            AlternateNodeName = "",
            ActiveFlag = "1"
          };
    
          RoleHierarchyDetails roleDetails = new RoleHierarchyDetails 
          { 
            RoleDetails = new RoleHierarchyDetail[] { roleHierarchyDetail }
          };
    
          XmlSerializer xml = new XmlSerializer(typeof(RoleHierarchyDetails));
          xml.Serialize(File.OpenWrite("roleHierarchyDetail.xml"), roleDetails);
        }
      }
    
      [XmlRoot(Namespace="http://cts.com/rhms/RoleHierarchyEvent")]
      public class RoleHierarchyDetails
      {
        [XmlElement(ElementName="RoleHierarchyDetails")]
        public RoleHierarchyDetail[] RoleDetails { get; set; }
      }
    
      public class RoleHierarchyDetail
      {
        [XmlElement]
        public string AssociateId { get; set; }
        [XmlElement]
        public string UserName { get; set; }
        [XmlElement]
        public string RoleId { get; set; }
        [XmlElement]
        public string RoleName { get; set; }
        [XmlElement]
        public string PortfolioName { get; set; }
        [XmlElement]
        public string PortfolioType { get; set; }
        [XmlElement]
        public string PortfolioId { get; set; }
        [XmlElement]
        public string HierarchyId { get; set; }
        [XmlElement]
        public string HierarchyName { get; set; }
        [XmlElement]
        public string AlternatePortfolioId { get; set; }
        [XmlElement]
        public string AlternateNodeName { get; set; }
        [XmlElement]
        public string ActiveFlag { get; set; }
      }
    }
    
    Thursday, March 03, 2011 10:43 AM
  • Below is one of the simplest methods to fulfill this requirement. Just use it to test first.

     XmlDocument newDoc = new XmlDocument();
     newDoc.LoadXml("<RoleHierarchyDetails xmlns=\"http://cts.com/rhms/RoleHierarchyEvent\"></RoleHierarchyDetails>");
     newDoc.PrependChild(newDoc.CreateXmlDeclaration("1.0", "utf-8", ""));
    
     XmlNode RoleHierarchyDetails = newDoc.GetElementsByTagName("RoleHierarchyDetails")[0];
    
     XmlNode RoleHierarchyDetail = newDoc.CreateElement("RoleHierarchyDetail");
     RoleHierarchyDetails.AppendChild(RoleHierarchyDetail);
    
     XmlElement AssociateID = newDoc.CreateElement("AssociateID");
     RoleHierarchyDetail.AppendChild(AssociateID);
     XmlElement UserName = newDoc.CreateElement("UserName");
     RoleHierarchyDetail.AppendChild(UserName);
     XmlElement RoleId = newDoc.CreateElement("RoleId");
     RoleHierarchyDetail.AppendChild(RoleId);
     XmlElement RoleName = newDoc.CreateElement("RoleName");
     RoleHierarchyDetail.AppendChild(RoleName);
     XmlElement PortfolioName = newDoc.CreateElement("PortfolioName");
     RoleHierarchyDetail.AppendChild(PortfolioName);
     XmlElement PortfolioType = newDoc.CreateElement("PortfolioType");
     RoleHierarchyDetail.AppendChild(PortfolioType);
     XmlElement PortfolioId = newDoc.CreateElement("PortfolioId");
     RoleHierarchyDetail.AppendChild(PortfolioId);
     XmlElement HierarchyId = newDoc.CreateElement("HierarchyId");
     RoleHierarchyDetail.AppendChild(HierarchyId);
     XmlElement HierarchyName = newDoc.CreateElement("HierarchyName");
     RoleHierarchyDetail.AppendChild(HierarchyName);
     XmlElement AlternatePortfolioId = newDoc.CreateElement("AlternatePortfolioId");
     RoleHierarchyDetail.AppendChild(AlternatePortfolioId);
     XmlElement AlternateNodeName = newDoc.CreateElement("AlternateNodeName");
     RoleHierarchyDetail.AppendChild(AlternateNodeName);
     XmlElement ActiveFlag = newDoc.CreateElement("ActiveFlag");
     RoleHierarchyDetail.AppendChild(ActiveFlag);
    
     AssociateID.InnerText = "";
     UserName.InnerText = "Sandeep Mutha";
     RoleId.InnerText = "R_ID_0018";
     RoleName.InnerText = "SBU2 Head";
     PortfolioName.InnerText = "AXA SWISS";
     PortfolioType.InnerText = "SBU2";
     PortfolioId.InnerText = "222";
     HierarchyId.InnerText = "HT_ID_001";
     HierarchyName.InnerText = "BU Hierarchy";
     AlternatePortfolioId.InnerText = "";
     AlternateNodeName.InnerText = "";
     ActiveFlag.InnerText = "1";
    
     newDoc.Save(@"C:\Users\v-xzliu\Desktop\RoleHierarchyDetails.xml");
    

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 04, 2011 3:51 AM

All replies

  • Hi All

    This is my xml document ,which i create manually,i need to create this document in c#.net

    <RoleHierarchyDetails xmlns="http://cts.com/rhms/RoleHierarchyEvent">
                <RoleHierarchyDetail>
                      <AssociateID></AssociateID>
                      <UserName>Sandeep Mutha</UserName>
                      <RoleId>R_ID_0018</RoleId>
                      <RoleName>SBU2 Head</RoleName>
                      <PortfolioName>AXA SWISS</PortfolioName>
                      <PortfolioType>SBU2</PortfolioType>
                      <PortfolioId>222</PortfolioId>
                      <HierarchyId>HT_ID_001</HierarchyId>
                      <HierarchyName>BU Hierarchy</HierarchyName>
                      <AlternatePortfolioId></AlternatePortfolioId>
                      <AlternateNodeName></AlternateNodeName>
                      <ActiveFlag>1</ActiveFlag>
                </RoleHierarchyDetail>
          </RoleHierarchyDetails>

    I need your help

    Thanks in Advance

    • Moved by edhickey Wednesday, March 02, 2011 5:41 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    • Merged by Leo Liu - MSFTModerator Friday, March 04, 2011 3:56 AM Totally the same.
    Tuesday, March 01, 2011 4:47 AM
  • Hello Balaswari,

    Please check this sample: http://www.csharphelp.com/2006/05/creating-a-xml-document-with-c/.

    Also, here's a C# project on how to read/write xml files: http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Thursday, March 03, 2011 10:04 AM
  • Hi you can create XML document using XML Serialization. Consider following C# method for converting object to XML string

    public string ToXML(Object oObject)
        {
          XmlDocument xmlDoc = new XmlDocument();
          XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
          using (MemoryStream xmlStream = new MemoryStream())
          {
            xmlSerializer.Serialize(xmlStream, oObject);
            xmlStream.Position = 0;
            xmlDoc.Load(xmlStream);
            return xmlDoc.InnerXml;
          }
        } 
    

    need to add following namespace for working with above method

    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    

    You can check following link

    *** http://www.c-sharpcorner.com/UploadFile/hasibul2363/5675/?ArticleID=fe08a5fa-2a34-4068-b11d-e66315e8d08e

    http://www.codeproject.com/KB/XML/GenericXmlSerialization.aspx

    http://msdn.microsoft.com/en-us/library/182eeyhh%28v=vs.71%29.aspx


    Hasibul Haque, MCTS http://blog.e-rains.com
    Thursday, March 03, 2011 10:37 AM
  • Hi

    http://www.csharphelp.com/2006/05/creating-a-xml-document-with-c/

    http://www.devx.com/tips/Tip/21168

    http://www.codekicks.com/2007/04/reading-and-writing-xml-files-using-c.html

    I think these could help you. Feel free to get back if you still have problems.


    Please mark the post as answer if it is helpfull to you - Hiran Repakula
    Thursday, March 03, 2011 10:39 AM
  • I'd do something like this - it will allow serialization and deserialization.
    using System;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication13
    {
      class Program
      {
        static void Main(string[] args)
        {
          RoleHierarchyDetail roleHierarchyDetail = new RoleHierarchyDetail()
          {
            AssociateId = "",
            UserName = "Sandeep Mutha",
            RoleId = "R_ID_0018",
            RoleName = "SBU2 Head",
            PortfolioName = "AXA SWISS",
            PortfolioType = "SBU2",
            PortfolioId = "222",
            HierarchyId = "HT_ID_001",
            HierarchyName = "BU Hierarchy",
            AlternatePortfolioId = "",
            AlternateNodeName = "",
            ActiveFlag = "1"
          };
    
          RoleHierarchyDetails roleDetails = new RoleHierarchyDetails 
          { 
            RoleDetails = new RoleHierarchyDetail[] { roleHierarchyDetail }
          };
    
          XmlSerializer xml = new XmlSerializer(typeof(RoleHierarchyDetails));
          xml.Serialize(File.OpenWrite("roleHierarchyDetail.xml"), roleDetails);
        }
      }
    
      [XmlRoot(Namespace="http://cts.com/rhms/RoleHierarchyEvent")]
      public class RoleHierarchyDetails
      {
        [XmlElement(ElementName="RoleHierarchyDetails")]
        public RoleHierarchyDetail[] RoleDetails { get; set; }
      }
    
      public class RoleHierarchyDetail
      {
        [XmlElement]
        public string AssociateId { get; set; }
        [XmlElement]
        public string UserName { get; set; }
        [XmlElement]
        public string RoleId { get; set; }
        [XmlElement]
        public string RoleName { get; set; }
        [XmlElement]
        public string PortfolioName { get; set; }
        [XmlElement]
        public string PortfolioType { get; set; }
        [XmlElement]
        public string PortfolioId { get; set; }
        [XmlElement]
        public string HierarchyId { get; set; }
        [XmlElement]
        public string HierarchyName { get; set; }
        [XmlElement]
        public string AlternatePortfolioId { get; set; }
        [XmlElement]
        public string AlternateNodeName { get; set; }
        [XmlElement]
        public string ActiveFlag { get; set; }
      }
    }
    
    Thursday, March 03, 2011 10:43 AM
  • From where do you get the names of the nodes and the inner text values? Or is it hard coded?
    Please Mark as Answered If this answers your question Or UnMark as Answered if it did not.
    Happy to Help :)
    My C# Blog
    My HTML Blog
    Thursday, March 03, 2011 10:43 AM
  • Below is one of the simplest methods to fulfill this requirement. Just use it to test first.

     XmlDocument newDoc = new XmlDocument();
     newDoc.LoadXml("<RoleHierarchyDetails xmlns=\"http://cts.com/rhms/RoleHierarchyEvent\"></RoleHierarchyDetails>");
     newDoc.PrependChild(newDoc.CreateXmlDeclaration("1.0", "utf-8", ""));
    
     XmlNode RoleHierarchyDetails = newDoc.GetElementsByTagName("RoleHierarchyDetails")[0];
    
     XmlNode RoleHierarchyDetail = newDoc.CreateElement("RoleHierarchyDetail");
     RoleHierarchyDetails.AppendChild(RoleHierarchyDetail);
    
     XmlElement AssociateID = newDoc.CreateElement("AssociateID");
     RoleHierarchyDetail.AppendChild(AssociateID);
     XmlElement UserName = newDoc.CreateElement("UserName");
     RoleHierarchyDetail.AppendChild(UserName);
     XmlElement RoleId = newDoc.CreateElement("RoleId");
     RoleHierarchyDetail.AppendChild(RoleId);
     XmlElement RoleName = newDoc.CreateElement("RoleName");
     RoleHierarchyDetail.AppendChild(RoleName);
     XmlElement PortfolioName = newDoc.CreateElement("PortfolioName");
     RoleHierarchyDetail.AppendChild(PortfolioName);
     XmlElement PortfolioType = newDoc.CreateElement("PortfolioType");
     RoleHierarchyDetail.AppendChild(PortfolioType);
     XmlElement PortfolioId = newDoc.CreateElement("PortfolioId");
     RoleHierarchyDetail.AppendChild(PortfolioId);
     XmlElement HierarchyId = newDoc.CreateElement("HierarchyId");
     RoleHierarchyDetail.AppendChild(HierarchyId);
     XmlElement HierarchyName = newDoc.CreateElement("HierarchyName");
     RoleHierarchyDetail.AppendChild(HierarchyName);
     XmlElement AlternatePortfolioId = newDoc.CreateElement("AlternatePortfolioId");
     RoleHierarchyDetail.AppendChild(AlternatePortfolioId);
     XmlElement AlternateNodeName = newDoc.CreateElement("AlternateNodeName");
     RoleHierarchyDetail.AppendChild(AlternateNodeName);
     XmlElement ActiveFlag = newDoc.CreateElement("ActiveFlag");
     RoleHierarchyDetail.AppendChild(ActiveFlag);
    
     AssociateID.InnerText = "";
     UserName.InnerText = "Sandeep Mutha";
     RoleId.InnerText = "R_ID_0018";
     RoleName.InnerText = "SBU2 Head";
     PortfolioName.InnerText = "AXA SWISS";
     PortfolioType.InnerText = "SBU2";
     PortfolioId.InnerText = "222";
     HierarchyId.InnerText = "HT_ID_001";
     HierarchyName.InnerText = "BU Hierarchy";
     AlternatePortfolioId.InnerText = "";
     AlternateNodeName.InnerText = "";
     ActiveFlag.InnerText = "1";
    
     newDoc.Save(@"C:\Users\v-xzliu\Desktop\RoleHierarchyDetails.xml");
    

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 04, 2011 3:51 AM
  • Thanks All

    I have  create xml using the below code

    XNamespace xmlns = "http://cts.com/rhms/RoleHierarchyEvent";

                XDocument xdoc = new XDocument(
                            new XElement(xmlns + "RoleHierarchyDetails",
                            new XElement(xmlns + "RoleHierarchyDetail",
                new XElement(xmlns + "AssociateID", RoleHierarchy[0].AssociateID),
                new XElement(xmlns + "UserName", RoleHierarchy[0].UserName),
                new XElement(xmlns + "RoleId", RoleHierarchy[0].RoleId),
                new XElement(xmlns + "RoleName", RoleHierarchy[0].RoleName),
                new XElement(xmlns + "PortfolioName", RoleHierarchy[0].PortfolioName),
                new XElement(xmlns + "PortfolioType", RoleHierarchy[0].PortfolioType),
                new XElement(xmlns + "PortfolioId", RoleHierarchy[0].PortfolioId),
                new XElement(xmlns + "HierarchyId", RoleHierarchy[0].HierarchyId),
                new XElement(xmlns + "HierarchyName", RoleHierarchy[0].HierarchyName),
                new XElement(xmlns + "AlternatePortfolioId", RoleHierarchy[0].AlternatePortfolioId),
                new XElement(xmlns + "AlternateNodeName", RoleHierarchy[0].AlternateNodeName),
                new XElement(xmlns + "ActiveFlag", RoleHierarchy[0].ActiveFlag)
                                        )
                                     )
                                   );

                var xmlDocument = new XmlDocument();
                using (var xmlReader = xdoc.CreateReader())
                {
                    xmlDocument.Load(xmlReader);
                }
              


    Balaswari
    Monday, June 06, 2011 12:43 PM