使用c#读取xml [英] reading xml using c#

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

问题描述



想要读取如下所示的xml文件:

 <   alarm-response-list     xmlns   =  http://www.ca.com/spectrum/restful/schema/response    错误  = < span class =code-keyword> EndOfResults    throttle   =  284   总报警  =  284 > ;  
< 警报响应 >
< alarm id = 50e1c57a-6860-100e-01a7-0019bb29739c >
< 属性 id = 0x1006e > ; 202.125.155.112 < / attribute >
< attribute id = 0x11f56 > 2 < / attribute >
< attribute id = 0x12b4c > 广域可能的链路故障< / attribute >
< 属性 id = 0x11f4e > 1356973434 < / attribute >
< attribute id = 0x1296e >
WA Link模型的联系状态(名称 - 202.125.155.112,类型 - WA_Link)处于禁用状态。
< / attribute >
< attribute id = 0x12d7f > 202.125.155.112 < / attribute < span class =code-keyword>>
< / alarm >
< alarm id = 510e5569-8843-1030-01a7-0019bb29739c >
< span class =code-keyword>< attribute id = 0x1006e > ZTBL-21271-DSL-Primary-Central < / attribute >
< attribute < span class =code-attribute> id = 0x11f56 > 1 < / attribute >
< attribute id = 0x12b4c > 检测到DUPLICATE MODEL < / attribute >
< attribute id = 0x11f4e > 1359893865 < / attribute >
< attribute id = 0x1296e >
Device ZTBL- 21271-DSL-Primary-Central类型Pingable具有一个或多个其他型号使用的IP地址(192.168.115.254)和MAC地址(0)。其他型号为:型号:NBP-NA-DSL-Primary-South类型:Pingable In:Universe:NBP South Zone
< / attribute >
< attribute id = 0x12d7f > 192.168.115.254 < / attribute >
< / alarm >



我需要使用c#获取标签之间的值。请帮我修改代码。



- 从OP评论中添加更多信息[CHill60]



 reader.ReadToFollowing(  alarm); 
reader.MoveToFirstAttribute();

/// /////////////// ////////////////////////////////////////////////
reader.ReadToFollowing( attribute);
string name = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 名称为: + name);
/// /////////////////// ////////////////////////////////////////////
/// ///////////////////// //////////////////////////////////////////
reader.ReadToFollowing( attribute);
string cause = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 严重性为: + cause);
/// /////////////////// ////////////////////////////////////////////
/// ///////////////////// //////////////////////////////////////////
reader.ReadToNextSibling( attribute);
string variab = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 原因是: + variab);
/// /////////////////// ////////////////////////////////////////////
/// ///////////////////// //////////////////////////////////////////
reader.ReadToFollowing( attribute);
string name1 = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 名称为: + name1);
/// /////////////////// ////////////////////////////////////////////
/// ///////////////////// //////////////////////////////////////////
reader.ReadToFollowing( attribute);
string cause1 = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 严重性为: + cause1);
/// /////////////////// ////////////////////////////////////////////
/// ///////////////////// //////////////////////////////////////////
reader.ReadToNextSibling( attribute);
string variab1 = reader.ReadElementContentAsString()。ToString();
MessageBox.Show( 原因是: + variab1);
/// /////////////////// ////////////////////////////////////////////





这是我到目前为止所做的,但这段代码正在跳过一种属性类型。输出是:

Quote:

202.125.155.112



< blockquote class =FQ>

Quote:

WIDE-AREA PROBABLE LINK FAILURE



引用:

202.125.155.112



然后它移动到下一个节点







XML来自网络服务。

解决方案

您好,



您有多种方法可以读取XML DOM:

1)使用XML Reader,速度非常快,并且不会消耗太多内存,但是不太开发人员友好

2)在XMLNode的SelectNodes / SelectSingleNode方法中使用XPath查询,该解决方案可能内存繁重但开发人员更友好

3)使用XML Navigator(实际上它是一个XPathNavigator)



根据你的要求,我会重新使用第一个或第二个选项进行修改。

要求将基于内存和效率限制。



希望此帮助

 reader.ReadToFollowing( 警报); 
reader.MoveToFirstAttribute();
for(int j = 0; j< = i; j ++)
{
////////////////////// //////////////////////////////////////////// $ b $读者。 ReadToFollowing( 属性);
string name = reader.ReadElementContentAsString()。ToString();
//MessageBox.Show(名称是:+名称);
////////////////////////////////////////////// ////////////////////
////////////////////////// ////////////////////////////////////////
reader.MoveToAttribute(attribute );
string severity = reader.ReadElementContentAsString()。ToString();
//MessageBox.Show(Severity is:+ severity);
////////////////////////////////////////////// ////////////////////
////////////////////////// ////////////////////////////////////////
reader.MoveToAttribute(attribute );
string cause = reader.ReadElementContentAsString()。ToString();
//MessageBox.Show(因为:+原因);
////////////////////////////////////////////// ////////////////////
////////////////////////// ////////////////////////////////////////
reader.MoveToAttribute(attribute );
string Time = reader.ReadElementContentAsString()。ToString();
double timestamp = Convert.ToDouble(Time);

System.DateTime dateTime = new System.DateTime(1970,1,1,0,0,0,0);

dateTime = dateTime.AddSeconds(timestamp);
dateTime = dateTime.AddHours(5);

string printDate = dateTime.ToShortDateString()+,+ dateTime.ToShortTimeString();

string [] arr = printDate.Split('','');

string date = arr [0];
string time = arr [1];


//MessageBox.Show(时间是:+时间+日期是:+日期);
////////////////////////////////////////////// ////////////////////
////////////////////////// ////////////////////////////////////////
reader.MoveToAttribute(attribute );
string nativ = reader.ReadElementContentAsString()。ToString();
//MessageBox.Show (\"NativeCause is:+ nativ);
////////////////////////////////////////////// ////////////////////
////////////////////////// ////////////////////////////////////////
reader.MoveToAttribute(attribute );
string ip = reader.ReadElementContentAsString()。ToString();
//MessageBox.Show(IP is:+ ip);
////////////////////////////////////////////// ////////////////////
inserthistory(name,0,nativ,date,time,ip);


Hi,
Going for something like reading an xml file which looks like this:

<alarm-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response" error="EndOfResults" throttle="284" total-alarms="284">
<alarm-responses>
<alarm id="50e1c57a-6860-100e-01a7-0019bb29739c">
<attribute id="0x1006e">202.125.155.112</attribute>
<attribute id="0x11f56">2</attribute>
<attribute id="0x12b4c">WIDE-AREA PROBABLE LINK FAILURE</attribute>
<attribute id="0x11f4e">1356973434</attribute>
<attribute id="0x1296e">
The contact status of WA Link model ( name - 202.125.155.112, type - WA_Link ) is in a "disabled" state.
</attribute>
<attribute id="0x12d7f">202.125.155.112</attribute>
</alarm>
<alarm id="510e5569-8843-1030-01a7-0019bb29739c">
<attribute id="0x1006e">ZTBL-21271-DSL-Primary-Central</attribute>
<attribute id="0x11f56">1</attribute>
<attribute id="0x12b4c">DUPLICATE MODEL DETECTED</attribute>
<attribute id="0x11f4e">1359893865</attribute>
<attribute id="0x1296e">
Device ZTBL-21271-DSL-Primary-Central of type Pingable has an IP address (192.168.115.254) and MAC address (0) that are used by one or more other models. The other models are: Model: NBP-NA-DSL-Primary-South Type: Pingable In: Universe:NBP South Zone
</attribute>
<attribute id="0x12d7f">192.168.115.254</attribute>
</alarm>


I need to get the values between the tags using c#. Please help me with the code.

[Edit] - Adding further information from OPs comments [CHill60]

reader.ReadToFollowing("alarm");
                   reader.MoveToFirstAttribute();

                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string name = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Name is: " + name);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string cause = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Severity is: " + cause);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToNextSibling("attribute");
                       string variab = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Cause is: " + variab);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string name1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Name is: " + name1);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToFollowing("attribute");
                       string cause1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Severity is: " + cause1);
                       //////////////////////////////////////////////////////////////////
                       //////////////////////////////////////////////////////////////////
                       reader.ReadToNextSibling("attribute");
                       string variab1 = reader.ReadElementContentAsString().ToString();
                       MessageBox.Show("Cause is: " + variab1);
                       //////////////////////////////////////////////////////////////////



this is what I have so far but this piece of code is skipping one attribute type. The output is:

Quote:

202.125.155.112


Quote:

WIDE-AREA PROBABLE LINK FAILURE


Quote:

202.125.155.112


and then it moves on to the next node



And the XML is coming from a web service.

解决方案

Hello,

You have multiple approaches for reading an XML DOM:
1) Use an XML Reader, very fast and doesn''t consume too much memory, but not too developer friendly
2) Use XPath queries in the SelectNodes / SelectSingleNode methods of XMLNode, that solution can be memory-heavy but is more developer friendly
3) Use an XML Navigator (actually it is an XPathNavigator)

Depending on your requirements, I''d recommend using either the first or the second options.
Requirements would be based upon the memory and efficiency constraints.

Hope this help


reader.ReadToFollowing("alarm");
                    reader.MoveToFirstAttribute();
                    for (int j = 0; j <= i; j++)
                    {
                        //////////////////////////////////////////////////////////////////
                        reader.ReadToFollowing("attribute");
                        string name = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Name is: " + name);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string severity = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Severity is: " + severity);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string cause = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("Cause is: " + cause);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string Time = reader.ReadElementContentAsString().ToString();
                        double timestamp = Convert.ToDouble(Time);

                        System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

                        dateTime = dateTime.AddSeconds(timestamp);
                        dateTime = dateTime.AddHours(5);

                        string printDate = dateTime.ToShortDateString() + " , " + dateTime.ToShortTimeString();

                        string[] arr = printDate.Split('','');

                        string date = arr[0];
                        string time = arr[1];


                        //MessageBox.Show("Time is:" + time + " Date is :" + date);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string nativ = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("NativeCause is: " + nativ);
                        //////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////
                        reader.MoveToAttribute("attribute");
                        string ip = reader.ReadElementContentAsString().ToString();
                        //MessageBox.Show("IP is: " + ip);
                        //////////////////////////////////////////////////////////////////
                        inserthistory(name, "0", nativ, date, time, ip);


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

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