无法从具有相同名称的XML读取第二列 [英] unable to read second column from XML with same name

查看:55
本文介绍了无法从具有相同名称的XML读取第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   ReferralMessages  >  
< 消息 > 拒绝/过时/导入的车辆未在线投保,请联系022-39898282或我们的任何分支机构以获取更多信息< < span class =code-leadattribute> / message >

< ; 消息 > 您将无法在线购买此政策,因为年龄车辆超过允许的限制。请联系我们或我们的任何分支机构以获取更多信息。< / message >

< / ReferralMessages < span class =code-keyword>>





我如何阅读第二列从XML到数据集的列名称相同。

解决方案

您可以像这样提取消息:

  string  xml =  @ < referralmessages> 
< message>拒绝/过时/进口车辆未在线投保,请联系022-39898282或我们的任何分支机构获取更多信息< / message>
< message>您将无法在线购买此政策,因为车辆的年龄超过允许的限制租赁联系人或我们的任何分支机构以获取更多信息。< / message>
< / referralmessages>
;

XDocument xDocument = XDocument.Parse(xml);

IEnumerable< XElement> xElements = xDocument.Descendants ( message);

foreach (XElement xElement in xElements)
{
string message = xElement.Value;
}


为了完整性,这里有两个使用 XDocument的替代方法访问你的xml字符串

 使用 System.Xml; 
< span class =code-keyword>使用 System.IO;
...
var x = XmlReader .Create( new StringReader(xml));
while (x。阅读())
Console.WriteLine (x.Value);





使用System.Xml; 
var xmlDoc = new XmlDocument ();
xmlDoc.LoadXml(xml);
foreach(XmlElement x in xmlDoc.GetElementsByTagName(message))
Console.WriteLine(x.InnerText);



您选择的方法将取决于你想要如何搜索文档(来回,直通,块等),以及你已经完成的事情(这就是我在评论中提到的原因)。



讨论这些解决方案中给出的3种方法(1和2)这里 [ ^ ]


<ReferralMessages>
                <message>Declined/Obsolete/Imported Vehicles are not Insured Online, Please contact 022-39898282 or any of our branches for more information </message>

                <message>You will not be able to buy this policy online, as the Age of the vehicle is more than the permissible limit.  Please contact  or any of our branches for more information.</message>

        </ReferralMessages>



how can i read Second column from XML to dataset if column name are same.

解决方案

You could extract the messages like this:

string xml = @"<referralmessages>
  <message>Declined/Obsolete/Imported Vehicles are not Insured Online, Please contact 022-39898282 or any of our branches for more information </message> 
  <message>You will not be able to buy this policy online, as the Age of the vehicle is more than the permissible limit.  Please contact  or any of our branches for more information.</message> 
  </referralmessages>";

XDocument xDocument = XDocument.Parse(xml);

IEnumerable<XElement> xElements = xDocument.Descendants("message");

foreach(XElement xElement in xElements)
{
    string message = xElement.Value;
}


In the interests of completeness here are two alternatives to using XDocument to access your xml string

using System.Xml;
using System.IO;
...
            var x = XmlReader.Create(new StringReader(xml));
            while (x.Read())
                Console.WriteLine(x.Value);


or

using System.Xml;
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            foreach (XmlElement x in xmlDoc.GetElementsByTagName("message"))
                Console.WriteLine(x.InnerText);


The method you choose will depend upon how you want to search the document (back and forth, straight through, in blocks etc.) and obviously on what you have already done (which is why I asked in the comment).

There is a discussion on the 3 methods given in these Solutions (1 and 2) here[^]


这篇关于无法从具有相同名称的XML读取第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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