Save List to XML file

后端 未结 6 1925
遥遥无期
遥遥无期 2021-02-05 13:15

I want to save records fetched from database in an XML file,
take x number of records from XML file into a custom collection List
process them and

相关标签:
6条回答
  • 2021-02-05 13:44

    You Can save Your List<T> into a DataTable then WriteXml

        T t0 = new T();
           t0.id=1;
           t0.property1="John";
           t0.property2="Doe";
        List<T> Tlist = new List<T>();
           Tlist.Add(t0);
    
        DataTable dt = new DataTable();
           dt.TableName = "People";
           dt.Columns.Add("ID");
           dt.Columns.Add("Name");
           dt.Columns.Add("Lastname");
    
    
            foreach(var item in tlist)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count-1]["ID"] = item.name;
                dt.Rows[dt.Rows.Count - 1]["Name"] = item.id_cod;
                dt.Rows[dt.Rows.Count - 1]["Lastname"] = item.id_cod;
    
    
            }
            dt.WriteXml("test.Xml");
    
    0 讨论(0)
  • 2021-02-05 13:46

    Here are two methods that we use to accomplish this using the XMLSerializer:

     public static T FromXML<T>(string xml)
     {
         using (StringReader stringReader = new StringReader(xml))
         {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
         }
     }
    
     public string ToXML<T>(T obj)
     {
        using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.Serialize(stringWriter, obj);
            return stringWriter.ToString();
        }
     }
    
    0 讨论(0)
  • 2021-02-05 13:46

    Using the code below (Class T Taken from your code snippet) you will be able to serialize into an XML file with ease, and without the hassle of implementing ISerializable

    [Serializable()]
    public class T
    {
        public int Id {get; set;}
        public string property1 {get; set;}
        public string property2 {get; set;}
    }
    
    ...
    
    List<T> data = new List<T>()
    
    ... // populate the list
    
    //create the serialiser to create the xml
    XmlSerializer serialiser = new XmlSerializer(typeof(List<T>));
    
    // Create the TextWriter for the serialiser to use
    TextWriter filestream = new StreamWriter(@"C:\output.xml");
    
    //write to the file
    serialiser.Serialize(filestream , data);
    
    // Close the file
    filestream.Close();
    
    0 讨论(0)
  • 2021-02-05 13:51

    Use the XmlSerializer class. Scroll down about 1/3 of the way for the examples.

    0 讨论(0)
  • 2021-02-05 13:58
    List<BI_QA_ChoiceEntity> choiceSet = new List<BI_QA_ChoiceEntity>();
            choiceSet = biEntityObj.ChoiceSet;
    
            XmlDocument ChoiceXML = new XmlDocument();
            ChoiceXML.AppendChild(ChoiceXML.CreateElement("CHOICESET"));
            foreach (var item in choiceSet)
            {
                XmlElement element = ChoiceXML.CreateElement("CHOICE");
               // element.AppendChild(ChoiceXML.CreateElement("CHOICE_ID")).InnerText = Convert.ToString(item.ChoiceID);
                element.AppendChild(ChoiceXML.CreateElement("CHOICE_TEXT")).InnerText = Convert.ToString(item.ChoiceText);
                element.AppendChild(ChoiceXML.CreateElement("SEQUENCE")).InnerText = Convert.ToString(item.Sequence);
                element.AppendChild(ChoiceXML.CreateElement("ISCORRECT")).InnerText = Convert.ToString(item.IsCorrect);
                ChoiceXML.DocumentElement.AppendChild(element);
            }
    

    Pass ChoiceXML to Stored procedure then SQL Server Do like as below

    @Choice_XML VARCHAR(MAX)=NULL
    
     IF(@Choice_XML<>'')  
                    BEGIN  
                        SET @intDocHandle =0
                        --Create an internal representation of the XML document.  
                        EXEC sp_xml_preparedocument @intDocHandle OUTPUT, 
    
    @Choice_XML  
    
                    --SET @ChoiceID = (SELECT  max([choice_id])+1 AS 'ChoiceID'  from BI_QUESTION_CHOICE)
    
                    --Insert 
                    INSERT BI_QUESTION_CHOICE
                    (
                        [choice_id],
                        [choice_descr],
                        [sequence],
                        [question_id],
                        [is_correct],
                        [created_by],
                        [created_dt],
                        [modified_by],
                        [modified_dt]
                    )
                    SELECT (SELECT  max([choice_id])+1 AS 'ChoiceID'  from BI_QUESTION_CHOICE),  
                        CASE WHEN CHOICE_TEXT='' THEN NULL ELSE CHOICE_TEXT END, 
                        CASE WHEN SEQUENCE='' THEN NULL ELSE SEQUENCE END,
                        QuestionID, 
                        CASE WHEN ISCORRECT='' THEN NULL ELSE ISCORRECT END,
                        'mbathini',  
                         GETDATE(),  
                        'mbathini', 
                        GETDATE()  
                    FROM OPENXML(@intDocHandle,'/CHOICESET/CHOICE', 3)  
                    WITH  
                    (CHOICE_TEXT VARCHAR(500), 
                     SEQUENCE VARCHAR(50),
                     QuestionID INT,
                     ISCORRECT bit)
    
    END
    
    0 讨论(0)
  • 2021-02-05 14:09

    While you could use a serializer - and many times this is the right answer - I personally would use Linq to XML which would allow you to be more flexible on how your XML should look like, i.e. to create the following XML from a collection foos based on your class:

    <Foos>
      <foo Id="1" property1="someprop1" property2="someprop2" />
      <foo Id="1" property1="another" property2="third" />
    </Foos>
    

    You could use:

    var xml = new XElement("Foos", foos.Select( x=> new XElement("foo", 
                                                    new XAttribute("Id", x.Id), 
                                                    new XAttribute("property1", x.property1), 
                                                    new XAttribute("property2", x.property2))));
    
    0 讨论(0)
提交回复
热议问题