如何使用C#将XMl文件转换为数据集? [英] How to convert a XMl File to dataset using C#?

查看:179
本文介绍了如何使用C#将XMl文件转换为数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何使用C#将XMl文件转换为DataTable?



I有Xml文件

----------------



Hi All,

How to convert a XMl File to DataTable using C#?

I have Xml file
----------------

<Destination Type="Country" ID="CN">
<Item Level="1" Title="Executive Summary" ID="DG-IG-EXECSUM">sample Text</Item>
</Destination>





在C#中代码:

------------

DataSet xmlDS = new DataSet ();

stream = new StringReader(xmlData);

//从流中加载XmlTextReader

reader = new XmlTextReader(stream) ;

xmlDS.ReadXml(xmlData,XmlReadMode.ReadSchema);



数据集我得到'Item'这样的表格

级别标题ID

1执行摘要DG-IG-EXECSUM



我需要这样的输出



级别标题ID值

1执行摘要DG-IG-EXECSUM示例文本





请指导我如何实现它。



In C# Code:
------------
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
// Load the XmlTextReader from the stream
reader = new XmlTextReader(stream);
xmlDS.ReadXml (xmlData,XmlReadMode.ReadSchema);

in dataset i got 'Item' table like this
Level Title ID
1 Executive Summary DG-IG-EXECSUM

I need output like this

Level Title ID Value
1 Executive Summary DG-IG-EXECSUM sample Text


please guide me how to implement it.

推荐答案

您好,



您可以使用以下代码添加节点的值。



Hi,

You can use below code for adding the value to the node.

XmlNode node = doc.SelectSingleNode("/Destination/Item");

            XmlAttribute xKey = doc.CreateAttribute("Value");
            xKey.Value = node.InnerText;

            node.Attributes.Append(xKey);





试试这个并告诉我你是否面对任何问题。



谢谢,

Sisir Patro



Try this and let me know if you are facing any issues.

Thanks,
Sisir Patro


这篇关于如何使用C#将XMl文件转换为数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆