【问题标题】:how can i convert xml document to data table in c#如何在c#中将xml文档转换为数据表
【发布时间】:2021-09-04 11:26:27
【问题描述】:

我从 api 作为 xml 得到以下响应

<ROOT>

<valid>1</valid>
<org_name> test org </org_name>
<count_of_vedios>3</count_of_vedios>
<total_length>351</total_length>

    <Video1>
        <Title>The Distinguished Gentleman</Title>
        <Director>Jonathan Lynn</Director>
        <Length>112 Minutes</Length>
        <Format>DVD</Format>
        <Rating>R</Rating>
    </Video1>
    <Video2>
        <Title>Her Alibi</Title>
        <Director>Bruce Beresford</Director>
        <Length>94 Mins</Length>
        <Format>DVD</Format>
        <Rating>PG-13</Rating>
    </Video2>
    <Video3>
        <Title>Chalte Chalte</Title>
        <Director>Aziz Mirza</Director>
        <Length>145 Mins</Length>
        <Format>DVD</Format>
        <Rating>N/R</Rating>
    </Video3>

</ROOT>

我需要用信息填写 aspx 页面的一些字段:
org_name : 测试组织
count_of_vedios : 3
总长度:351

我还需要用节点中的 vedios 填充 gridview

标题 |导演 |长度 |格式 |评分 |

【问题讨论】:

标签: c# asp.net xml


【解决方案1】:

数据集在这里有 4 个表,所以我所做的就是用 vedio 替换 vedio# 标记,现在数据集将有 2 个表,一个用于一般节点,另一个用于 vedios

DataTable general_dt = new DataTable();
DataTable vedios_dt = new DataTable();
string xml_str = document.DocumentElement.InnerXml.ToString();
                xml_str = Regex.Replace(xml_str, @"<Video[0-9]{1,3}>", "<Video>");
                xml_str = Regex.Replace(xml_str, @"<\/Video[0-9]{1,3}>", "</Video>");

                DataSet ds = new DataSet();
                ds.ReadXml(new System.IO.StringReader(xml_str));

                if (ds.Tables.Count > 0)
                {
                    XmlNode newnode;
                    newnode = document.ReadNode(new XmlTextReader(new StringReader(xml_str)));
                    if (newnode["valid"].InnerText.ToString() == "1")
                    {
                        general_dt = ds.Tables[0];
                        
                        vedios_dt = ds.Tables[1];
                    }
        }````

【讨论】:

    【解决方案2】:

    试试 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("Title", typeof(string));
                dt.Columns.Add("Directory", typeof(string));
                dt.Columns.Add("Length", typeof(string));
                dt.Columns.Add("Format", typeof(string));
                dt.Columns.Add("Rating", typeof(string));
    
                XDocument doc = XDocument.Load(FILENAME);
                List<XElement> videos = doc.Root.Elements().Where(x => x.Name.LocalName.StartsWith("Video")).ToList();
    
                foreach (XElement video in videos)
                {
                    dt.Rows.Add(new object[] {
                        (string)video.Element("Title"),
                        (string)video.Element("Director"),
                        (string)video.Element("Length"),
                        (string)video.Element("Format"),
                        (string)video.Element("Rating")
                    });
    
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多