如何使用C#将XML读取到数据集中

时间:2018-06-28 07:43:03

标签: c# asp.net xml dataset linq-to-xml

我正在尝试读取以下XML,但是在执行读取操作时遇到以下异常。 实际上,两行的XML结构都不相同,那么该怎么办?如果有人帮助我,我将不胜感激。

例外:无法将简单内容列添加到包含元素列或嵌套关系的表中。

XML代码:

<Response>
    <MemberSummary>
      <Age>39</Age>      
      <DateOfBirth>06:07:1985:00:00</DateOfBirth>
      <EmailAddress>abc@rentacar3.com</EmailAddress>      
      <MobilePhone>
        <CountryCode>1</CountryCode>        
        <Number>2049515487</Number>
      </MobilePhone>      
      <WorkPhone>
        <CountryCode>93</CountryCode>        
        <Number>1921525542</Number>
      </WorkPhone>      
    </MemberSummary>

    <MemberSummary>
      <Age>29</Age>      
      <DateOfBirth>06:07:1989:00:00</DateOfBirth>
      <EmailAddress>abc@rentacar2.com</EmailAddress>      
      <MobilePhone>
        <CountryCode>1</CountryCode>        
        <Number>2049515949</Number>
      </MobilePhone>      
      <WorkPhone>
        <CountryCode>93</CountryCode>        
        <Number>1921525125</Number>
      </WorkPhone> 
      <HomePhone>
        <CountryCode>213</CountryCode>       
        <Number>8182879870</Number>
      </HomePhone>      
    </MemberSummary>
</Response>

读取XML的代码:

    XDocument x = gn.WebService(request, "members");//get data from api
    XmlDocument xdoc = new XmlDocument();
    using (var xmlReader = x.CreateReader())
     {
         xdoc.Load(xmlReader);
     } 

    DataSet ds = new DataSet();
    ds.ReadXml(new XmlNodeReader(xdoc)); // From here throws above exception

3 个答案:

答案 0 :(得分:1)

这是正确的阅读方式:

XDocument doc = XDocument.Load("xml.xml"); //Loads the document by looking for the given path
List<XElement> elements = doc.Descendants().ToList(); //Extracts a list of every descendant node of the starting element.

在此示例中,我要从文档中获取后代,这样它将获得每个子节点。

如果您想要特定的内容,则必须逐个阅读,最好使用Elements()而不是Descendants()

Elements()将直接为您提供子节点


在评论中回答OP后进行编辑:

通过这种方式,您可以将成员转换为类,从而可以使用您想要的每种格式输出数据:

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load("xml.xml");
        XElement response = doc.Elements().FirstOrDefault();
        List<XElement> xmlMemberSummaries = response.Elements().ToList();
        List<MemberSummary> memberSummaries = xmlMemberSummaries.Select(x => Deserialize<MemberSummary>(x.ToString())).ToList();
    }

    public static string Serialize<T>(T dataToSerialize)
    {
        try
        {
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stringwriter, dataToSerialize);
            return stringwriter.ToString();
        }
        catch
        {
            throw;
        }
    }

    public static T Deserialize<T>(string xmlText)
    {
        try
        {
            var stringReader = new System.IO.StringReader(xmlText);
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
        }
        catch
        {
            throw;
        }
    }
}


public class MemberSummary
{
    public int Age { get; set; }
    public string DateOfBirth { get; set; }
    public string EmailAddress { get; set; }
    public MobilePhone MobilePhone { get; set; }
    public WorkPhone WorkPhone { get; set; }
    public HomePhone HomePhone { get; set; }
}

public interface Phone
{
    string CountryCode { get; set; }
    string Number { get; set; }
}

public class MobilePhone : Phone
{
    public string CountryCode { get; set; }
    public string Number { get; set; }
}

public class WorkPhone : Phone
{
    public string CountryCode { get; set; }
    public string Number { get; set; }
}

public class HomePhone : Phone
{
    public string CountryCode { get; set; }
    public string Number { get; set; }
}

答案 1 :(得分:0)

如果您想读取xml文件,我认为您可以使用名为ClosedXml的块。  我曾经这样做的代码:

        XLWorkbook workbook;
        workbook = null;
        try
        {

            var filename = yourpath.xml
            workbook = new XLWorkbook(filenamee);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error");
            return;
        }
        bool sucesso = workbook.Worksheets.TryGetWorksheet("Célula", out IXLWorksheet ws);
        if (!sucesso)
        {
            MessageBox.Show("Not possible to Open the file.");
            return;
        }

        foreach (var row in ws.RowsUsed()) //rowsused()
        {
            UA ua = new UA { };
            ua.DistritoCod = row.Cell(1).GetString();
            ua.Distrito = row.Cell(2).GetString();
            ua.ConcelhoCod = row.Cell(3).GetString();
            ua.Concelho = row.Cell(4).GetString();
            ua.IDEndereço = row.Cell(5).GetString();
            ua.TipoDeRua = row.Cell(6).GetString();
            ua.Endereço = row.Cell(7).GetString();
            ua.IDLocalidade = row.Cell(8).GetString();
            ua.Localidade = row.Cell(9).GetString();
            ua.CP4 = row.Cell(10).GetString();
            ua.CP3 = row.Cell(11).GetString();
            ua.NumPolicia = row.Cell(12).GetString();
            ua.Andar = row.Cell(13).GetString();
            ua.Fracçao = row.Cell(14).GetString();
            ua.Edificio_cad = row.Cell(17).GetString();
            ua.Celula = row.Cell(18).GetString();
            ua.Latitude = row.Cell(21).GetString().Replace(",", ".");
            ua.Longitude = row.Cell(22).GetString().Replace(",", ".");
            ua.Tpo_HP = row.Cell(24).GetString();
            ua.NomeEmpresa = row.Cell(25).GetString();
            ua.ValidaçaoMoradas = row.Cell(28).GetString();
            ua.Comentario = row.Cell(29).GetString();

            listagem.Add(ua);


            if (edificios.FindAll(x => x.id == ua.Edificio_cad).Count == 0)
            {
                edificios.Add(item: new { id = ua.Edificio_cad, lat = ua.Latitude, lon = ua.Longitude });
            }

        }
        dataGridView1.DataSource = listagem;

这里的列表是:

    List<UA> listagem = new List<UA>();
    public string currentmap = "http://maps.google.com/maps?=";
    public string lat;
    public string lon;
    public string resulttestt;


    public class UA
    {
        public string DistritoCod { get; set; }
        public string Distrito { get; set; }
        public string ConcelhoCod { get; set; }
        public string Concelho { get; set; }
        public string IDEndereço { get; set; }
        public string TipoDeRua { get; set; }
        public string Endereço { get; set; }
        public string IDLocalidade { get; set; }
        public string Localidade { get; set; }
        public string CP4 { get; set; }
        public string CP3 { get; set; }
        public string NumPolicia { get; set; }
        public string Andar { get; set; }
        public string Fracçao { get; set; }
        public string Edificio_cad { get; set; }
        public string Celula { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string Tpo_HP { get; set; }
        public string NomeEmpresa { get; set; }
        public string ValidaçaoMoradas { get; set; }
        public string Comentario { get; set; }
        public UA() { }
    }

希望这可以解决您的问题,让我知道!

答案 2 :(得分:0)

您不需要输入中的所有标签,因此在这种情况下,我将使用xml linq并将数据放入单个数据表中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Birth", typeof(DateTime));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("Mobile Code", typeof(int));
            dt.Columns.Add("Mobile Phone", typeof(string));
            dt.Columns.Add("Work Code", typeof(int));
            dt.Columns.Add("Work Phone", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement row in doc.Descendants("MemberSummary"))
            {
                DataRow newRow = dt.Rows.Add();

                newRow["Age"] = (int)row.Element("Age");
                newRow["Birth"] = DateTime.ParseExact((string)row.Element("DateOfBirth"), "MM:dd:yyyy:HH:mm", System.Globalization.CultureInfo.InvariantCulture);
                newRow["Email"] = (string)row.Element("EmailAddress");
                newRow["Mobile Code"] = (int)row.Element("MobilePhone").Element("CountryCode");
                newRow["Mobile Phone"] = (string)row.Element("MobilePhone").Element("Number");
                newRow["Work Code"] = (int)row.Element("WorkPhone").Element("CountryCode");
                newRow["Work Phone"] = (string)row.Element("WorkPhone").Element("Number");
            }

        }
    }
}