如何在c#,。net中读取xml [英] How to read xml in c#, .net

查看:76
本文介绍了如何在c#,。net中读取xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想知道服务返回的XML数据的读取情况。问题是,我已经实现了服务,服务正在以XML格式返回数据。



我想读取XML并获取数据。



第一服务:返回单值记录



< response>< status> 1< / status>< activated> 1< / activated>< / response>



需要阅读状态并已激活



第二次服务:返回多个价值记录



< response>

< status> 1< / status>

< request_id> ; 185< / request_id>

< timestamp>

<![CDATA [2014-03-27 02:46:07]]>

< / timestamp>

< ads>

< ad>

< campaign_id> 1< / campaign_id>

< ad_id> 1< / ad_id>

< ad_type />

< ad_size />

< ad_duration />

< / ad>

< ad>

< campaign_id> 3< / campaign_id> ;

< ad_id> 8< / ad _id>

< ad_type />

< ad_size />

< ad_duration />

< / ad>

< ad>

< campaign_id> 2< / campaign_id>

< ad_id> 14< / ad_id>

< ad_type />

< ad_size />

< ad_duration />

< / ad>

< ad>

< campaign_id> 11< / campaign_id>

< ad_id> 16< ; / ad_id>

< ad_type />

< ad_size />

< ad_duration />

< / ad>

< ad>

< campaign_id> 12< / campaign_id>

< ad_id> 18< / ad_id>

< ad_type />

< ad_size />

< ad_duration />

< / ad>

< ad>

< campaign_id> 13< / campaign_id>

< ad_id> ; 20< / ad_id>

< ad_type />

< ad_size />

< ad_duration /&g t;

< / ad>

< / ads>

< / response>



我需要阅读广告和广告系列ID值





如何在运行时阅读此内容。 />


请提供代码。



谢谢

解决方案

学会自己动手: c-tutorial-reading-和写作xml文件 [ ^ ]


1。您可以使用xmlreader读取所有数据。

我没有相关经验。但也许这个链接可以帮助你

XmlReader示例 [ ^ ]



2.您可以使用对象序列化和反序列化



第一个对象看起来像

  public  响应
{
public int status { get ; set ; }
public int 已激活{获取; set ; }

public response(){}
}



然后你可以做

 XmlSerializer mySerializer =  new  XmlSerializer( typeof运算(响应)); 
使用(FileStream stream = new FileStream(filepath,FileMode.Open))
{
return (响应)mySerializer.Deserialize(stream);
}









3. Linq to sql

使用linq to sql的示例 [ ^ ]



4. XmlDocument

ReadingXml使用XmlDocument [ ^ ]



可能还有更多选择


Hi,

I would like to know about the reading of XML data returned by the services. The problem is, I have implemented the services and the services are returning the data in XML format.

I want to read that XML and fetch the data.

First Service: Returning Single Value Record

<response><status>1</status><activated>1</activated></response>

Need to read Status and Activated

Second Service: Returning Multiple Value Records

<response>
<status>1</status>
<request_id>185</request_id>
<timestamp>
<![CDATA[ 2014-03-27 02:46:07 ]]>
</timestamp>
<ads>
<ad>
<campaign_id>1</campaign_id>
<ad_id>1</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>3</campaign_id>
<ad_id>8</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>2</campaign_id>
<ad_id>14</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>11</campaign_id>
<ad_id>16</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>12</campaign_id>
<ad_id>18</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>13</campaign_id>
<ad_id>20</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
</ads>
</response>

I need to read ADs and Campaign id values


How can I read this during runtime.

Please provide a code.

Thanks

解决方案

Learn to do it yourself: c-tutorial-reading-and-writing-xml-files[^]


1. You could read all the data with like xmlreader.
I don't have experience with that. but maybe this link will help you
XmlReader example[^]

2. you could use Object serialize and deserialize

first object would look like

public class response
{
    public int status { get; set; }
    public int activated { get; set; }

    public response() { }
}


and then you could do

XmlSerializer mySerializer = new XmlSerializer(typeof(response));
            using (FileStream stream = new FileStream(filepath, FileMode.Open))
            {
                return (response)mySerializer.Deserialize(stream);
            }





3. Linq to sql
Example using linq to sql[^]

4. XmlDocument
ReadingXml Using XmlDocument[^]

probably there are even more options


这篇关于如何在c#,。net中读取xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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