我想保存从XML文件中的数据库获取的记录,
将XML文件中的x条记录转换为自定义集合List<T>
处理它们并将更新的项目保存回XML文件。
'T'是一个具有值类型属性的简单对象,类似于 -
public class T
{
public int Id {get; set;}
public string property1 {get; set;}
public string property2 {get; set;}
}
请指导我如何将自定义集合List<T>
保存到XML文件,反之亦然?
另外,因为我没有发送这个XML文件,所以在某些回复中建议使用XmlSerializer是否有意义?
答案 0 :(得分:16)
以下是使用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();
}
}
答案 1 :(得分:14)
虽然您可以使用序列化程序 - 而且很多时候这是正确的答案 - 我个人会使用Linq to XML,这将允许您更灵活地了解XML的外观,即从一个XML创建以下XML根据您的班级收集foos
:
<Foos>
<foo Id="1" property1="someprop1" property2="someprop2" />
<foo Id="1" property1="another" property2="third" />
</Foos>
您可以使用:
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))));
答案 2 :(得分:9)
使用下面的代码(从您的代码片段中获取的T类),您将能够轻松地序列化到xml文件中,而且不会出现使用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();
答案 3 :(得分:2)
使用XmlSerializer课程。向下滚动大约1/3的示例。
答案 4 :(得分:0)
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);
}
将ChoiceXML传递给存储过程然后SQL Server如下所示
@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
答案 5 :(得分:-1)
您可以将List<T>
保存到DataTable然后保存为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");