必须将数据从 MS SQL SERVER DB 导出到 xml。
XML结构(导入文件):
<?xml version="1.0" encoding="utf-8"?>
<DOCTORS>
<DOCTOR>
<surname>Никитина</surname>
<name>Нина</name>
<patronymic>Петровна</patronymic>
<profession>Терапевт</profession>
<category>1</category>
<PATIENT>
<surname>Понамарев</surname>
<name>Олег</name>
<patronymic>Игнатьевич</patronymic>
<date_birth>12.02.1997</date_birth>
<category>Инвалид</category>
<NOTES>
<date_note>01.03.2020</date_note>
<diagnos>ОРВИ</diagnos>
<price>560</price>
</NOTES>
</PATIENT>
<PATIENT>
<surname>Николаев</surname>
<name>Георгий</name>
<patronymic>Николаевич</patronymic>
<date_birth>17.11.1985</date_birth>
</PATIENT>
</DOCTOR>
<DOCTOR>
<surname>Романовна</surname>
<name>Светлана</name>
<patronymic>Николаевна</patronymic>
<profession>Дерматолог</profession>
<category>6</category>
</DOCTOR>
</DOCTORS>
医生班:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WindowsFormsApp1
{
[XmlRoot(ElementName = "DOCTORS")]
public class DOCTORS
{
[XmlElement(ElementName = "DOCTOR")]
public List<DoctorXML> DOCTOR { get; set; }
}
}
DoctorXML 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WindowsFormsApp1
{
[XmlRoot(ElementName = "DOCTOR")]
public class DoctorXML
{
[XmlElement(ElementName = "surname")]
public string Surname { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "patronymic")]
public string Patronymic { get; set; }
[XmlElement(ElementName = "profession")]
public string Profession { get; set; }
[XmlElement(ElementName = "category")]
public string Category { get; set; }
[XmlElement(ElementName = "PATIENT")]
public List<PATIENTXML> PATIENT { get; set; }
}
}
PatientXML 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WindowsFormsApp1
{
[XmlRoot(ElementName = "PATIENT")]
public class PATIENTXML
{
[XmlElement(ElementName = "surname")]
public string Surname { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "patronymic")]
public string Patronymic { get; set; }
[XmlElement(ElementName = "date_birth")]
public string Date_birth { get; set; }
[XmlElement(ElementName = "category")]
public string Category { get; set; }
[XmlElement(ElementName = "NOTES")]
public NOTESXML NOTES { get; set; }
}
}
NotesXML 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WindowsFormsApp1
{
[XmlRoot(ElementName = "NOTES")]
public class NOTESXML
{
[XmlElement(ElementName = "date_note")]
public string Date_note { get; set; }
[XmlElement(ElementName = "diagnos")]
public string Diagnos { get; set; }
[XmlElement(ElementName = "price")]
public string Price { get; set; }
}
}
XmlSerialization 类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace WindowsFormsApp1
{
class XmlSerialization<T>
{
private Object _obj = typeof(T);
public Object ReadData(string filePath, Object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
if (File.Exists(filePath))
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
XmlSerializer xser = new XmlSerializer(typeof(T));
obj = (T)xser.Deserialize(fs);
fs.Close();
}
return obj;
}
throw new Exception("file does not exist");
}
public void WriteData(string filePath, Object obj)
{
if (File.Exists(filePath)) File.Delete(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
XmlSerializer xser = new XmlSerializer(typeof(T));
xser.Serialize(fs, obj);
fs.Close();
}
}
}
}
问题:
我知道您需要使用 WriteData 方法,但是如何从数据库中获取信息,使其具有与文件导入本身相同的结构?毕竟,如果我只写一个请求
var q = context.Doctors.Select()...
,那么我将收到非结构化的信息。
SQL Server 具有强大的 XML 功能。
Решение только в T-SQL. Одна команда T-SQL ... Очень просто.
Проверьте это в SQL Server Management Studio (SSMS).
如果您更正了 XML 中的错误,那么它应该是这样的
要读取和写入这样的文件,您需要以下类
你没有提供你的数据库模式,我创建了一个这样的
然后,要获得具有专利和诊断的医生的集合,您需要这个类
您可以像这样使用这些类
完整的项目在这里