【问题标题】:Read XML file and save tag values in Oracle table读取 XML 文件并将标记值保存在 Oracle 表中
【发布时间】:2019-01-30 17:05:06
【问题描述】:

我有一个这样的 XML 文件:

<billing_documents>
    <billing_document>
        <Field1>AAAAA</Field1>
        <Field2>BBBBB</Field2>
        <Field3>20180731</Field3>
        <Field4/>
    <billing_document>
<billing_documents>

(某些字段可能为空,如本例中的 Field4),

我需要阅读它并将“FieldX”值保存在 Oracle 表中 具有相同的结构。

字段 1 - 字段 2 - 字段 3 - 字段 4

谁能帮帮我?

提前非常感谢。

路易斯

PS 我正在使用 Visual Studio 2017。

【问题讨论】:

标签: c# xml oracle


【解决方案1】:

尝试使用 xml linq。我将结果放入数据表中。您可以根据需要进行修改以将结果存储在 oracle 数据库中

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

namespace ConsoleApplication1
{
    public class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        private static void Main()
        {
            XDocument doc = XDocument .Load(FILENAME);

            DataTable dt = new DataTable();
            dt.Columns.Add("Field1", typeof(string));
            dt.Columns.Add("Field2", typeof(string));
            dt.Columns.Add("Field3", typeof(string));
            dt.Columns.Add("Field4", typeof(string));

            foreach (XElement document in doc.Descendants("billing_document"))
            {
                dt.Rows.Add(new object[] {
                    (string)document.Element("Field1"),
                    (string)document.Element("Field2"),
                    (string)document.Element("Field3"),
                    (string)document.Element("Field4")
                });
            }
        }

    }

}

【讨论】:

  • 谢谢jdweeng,我就这样试试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 2021-09-28
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多