且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在c#,。net中读取xml

更新时间:2023-11-24 12:21:04

学会自己动手: 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