各位老师,如何,把一个xml文件(有父节点和子节点)读到sql中,用c#程序请给一个完整的例子和代码??
2个回答
展开全部
public static List<StudentRepairsMassage> SetRepairsXML()
{
List<StudentRepairsMassage> list = new List<StudentRepairsMassage>();
string RepairsXMLPath = ConfigurationManager.AppSettings["RespairsXMLPath"];
if (!File.Exists(RepairsXMLPath))//如果文件不存在,则创建
{
string strfinename = RepairsXMLPath;
XmlTextWriter xmlWriter = new XmlTextWriter(strfinename, Encoding.Default);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees");
xmlWriter.Close();
return list;
}
XmlDocument doc = new XmlDocument();
doc.Load(RepairsXMLPath); //加载Xml文件
XmlElement rootElem = doc.DocumentElement; //获取根节点
XmlNodeList personNodes = rootElem.GetElementsByTagName("Node"); //获取Employees子节点集合
foreach (XmlNode node in personNodes)
{
StudentRepairsMassage student = new StudentRepairsMassage();
XmlElement xml = (XmlElement)node;
XmlNodeList xml1 = xml.ChildNodes;
student.title = xml1.Item(0).InnerText;
student.id = xml1.Item(1).InnerText;
student.address = xml1.Item(2).InnerText;
student.article = xml1.Item(3).InnerText;
student.message = xml1.Item(4).InnerText;
list.Add(student);
}
return list;
}
}
public class StudentRepairsMassage
{
public string title { get; set; }
public string id { get; set; }
public string address { get; set; }
public string article { get; set; }
public string message { get; set; }
}
这是读取xml的,写入sql就用先ADO.NET,然后sql写入,不知道就直接动软吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询