无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”。---C#

最近自己负责维护的一个软件出现了如下错误:

Exception: 无法将类型为“System.Xml.XmlComment”的对象强制转换为类型“System.Xml.XmlElement”。

收到产品线发来的bug通知,我很快就意识到了在什么地方出错。之前公司软件升级时,有一个xml文件需要我们plc这边负责升级(由原来的lua文件格式转为xml格式)。解析xml的代码部分如下:

 var doc = new XmlDocument();
 doc.Load(Path)         
 foreach (XmlElement childNode in doc.DocumentElement.ChildNodes)
    {
         if(childNode.Name == "I")
           {
               .........
           }  
          .........
    }

待解析的xml文件的内容格式为:

<?xml version="1.0" encoding="utf-8"?>
<IO>
   <I L="xxx.yyy" R="bbb.ccc" />
   <I L="ccc.ddd" R="ttt.iii" />
  <O L="rrr.eee" R="eee.ccc" />
   <O L="ppp.eee" R="vvv.ccc" />
</IO>

按理来说,待解析的xml文件只要符合上述的格式,都是可以解析成功的。之前也是做了非常“充分”的测试,所以组件的alpha(不能保证没有bug的)版本就发出去了,奈何这个alpha版本组件在测试部门的测试下也没有找到这个隐藏的bug。那到底是怎么样才会出现上述的bug呢。

非常简单的重现该bug就是在解析的xml文件中进行注释内容的添加:

<?xml version="1.0" encoding="utf-8"?>
<IO>
   <I L="xxx.yyy" R="bbb.ccc" />
   <!--<I L="ccc.ddd" R="ttt.iii" />
  <O L="rrr.eee" R="eee.ccc" />-->
   <O L="ppp.eee" R="vvv.ccc" />
</IO>

这个添加注释的操作是合理的,但是在代码中解析时却是“不合理的”,因为doc去解析xml获得的ChildNodes集合中并非所有元素的类型都是XmlElement。就拿注释的内容来说,它就是XmlComment类型。于是乎,用户输入的xml文件内容包含非XmlElement类型的子节点时发生了报错。

那么怎么改呢,超级简单,只要用foreach去遍历ChildNodes子节点时,我先用object类型变量node去获取,然后转为XmlElement类型变量childNode,转换成功则继续(childNode不为null),否则跳过当前循环。

 var doc = new XmlDocument();
 doc.Load(Path)         
 foreach (object node in doc.DocumentElement.ChildNodes)
    {
         XmlElement childNode = childNode as XmlElement;
         if(childNode == null)
             continue;

         if(childNode.Name == "I")
           {
               .........
           }  
          .........
    }

总结一下:遇到遍历某个集合的逻辑时,先判断自己要处理的是什么类型,以及集合里面大概都有什么类型。上述修改的做法是最可靠的,不管你集合里面有什么类型,我只关心我要处理的类型,否则pass掉。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将XML请求转换为application/x-www-form-urlencoded模型,您需要对XML请求进行解析,并将其转换为.NET对象。然后,您可以使用.NET的内置方法将.NET对象转换为application/x-www-form-urlencoded格式的字符串。 以下是一个示例,演示如何使用XmlSerializer将XML请求转换为.NET对象,并使用System.Net.Http.Formatting库将.NET对象转换为application/x-www-form-urlencoded格式的字符串: ```csharp using System.Net.Http.Formatting; using System.Xml.Serialization; // 定义XML请求实体类 [XmlRoot("OTA_InventoryCheckRQ")] public class InventoryCheckRequest { [XmlAttribute("EchoToken")] public string EchoToken { get; set; } [XmlAttribute("Password")] public string Password { get; set; } [XmlAttribute("PrimaryLangID")] public string PrimaryLangID { get; set; } [XmlAttribute("TimeStamp")] public string TimeStamp { get; set; } [XmlAttribute("UserName")] public string UserName { get; set; } [XmlAttribute("Version")] public string Version { get; set; } [XmlElement("HotelReservations")] public HotelReservations HotelReservations { get; set; } } public class HotelReservations { [XmlElement("HotelReservation")] public HotelReservation HotelReservation { get; set; } } public class HotelReservation { [XmlElement("RoomStay")] public RoomStay RoomStay { get; set; } [XmlElement("ResGlobalInfo")] public ResGlobalInfo ResGlobalInfo { get; set; } } public class RoomStay { [XmlElement("RoomTypes")] public RoomTypes RoomTypes { get; set; } [XmlElement("RatePlans")] public RatePlans RatePlans { get; set; } [XmlElement("GuestCounts")] public GuestCounts GuestCounts { get; set; } [XmlElement("BasicPropertyInfo")] public BasicPropertyInfo BasicPropertyInfo { get; set; } } public class RoomTypes { [XmlElement("RoomType")] public RoomType RoomType { get; set; } } public class RoomType { [XmlAttribute("RoomTypeCode")] public string RoomTypeCode { get; set; } } public class RatePlans { [XmlElement("RatePlan")] public RatePlan RatePlan { get; set; } } public class RatePlan { [XmlAttribute("RatePlanCode")] public string RatePlanCode { get; set; } } public class GuestCounts { [XmlElement("GuestCount")] public List<GuestCount> GuestCount { get; set; } } public class GuestCount { [XmlAttribute("AgeQualifyingCode")] public string AgeQualifyingCode { get; set; } [XmlAttribute("Count")] public int Count { get; set; } } public class BasicPropertyInfo { [XmlAttribute("HotelCode")] public string HotelCode { get; set; } } public class ResGlobalInfo { [XmlElement("RoomCount")] public int RoomCount { get; set; } [XmlElement("MemberLevel")] public string MemberLevel { get; set; } [XmlElement("TimeSpan")] public TimeSpan TimeSpan { get; set; } } public class TimeSpan { [XmlAttribute("End")] public string End { get; set; } [XmlAttribute("Start")] public string Start { get; set; } } // 在控制器中处理请求 public async Task<IActionResult> ProcessRequest([FromBody] string xmlRequest) { // 解析XML请求 XmlSerializer serializer = new XmlSerializer(typeof(InventoryCheckRequest)); InventoryCheckRequest request; using (StringReader reader = new StringReader(xmlRequest)) { request = (InventoryCheckRequest)serializer.Deserialize(reader); } // 将.NET对象转换为application/x-www-form-urlencoded格式的字符串 var formData = new FormUrlEncodedContent(new Dictionary<string, string> { { "EchoToken", request.EchoToken }, { "Password", request.Password }, { "PrimaryLangID", request.PrimaryLangID }, { "TimeStamp", request.TimeStamp }, { "UserName", request.UserName }, { "Version", request.Version }, { "HotelReservations.HotelReservation.RoomStay.RoomTypes.RoomType.RoomTypeCode", request.HotelReservations.HotelReservation.RoomStay.RoomTypes.RoomType.RoomTypeCode }, { "HotelReservations.HotelReservation.RoomStay.RatePlans.RatePlan.RatePlanCode", request.HotelReservations.HotelReservation.RoomStay.RatePlans.RatePlan.RatePlanCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].AgeQualifyingCode", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].AgeQualifyingCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].Count", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[0].Count.ToString() }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].AgeQualifyingCode", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].AgeQualifyingCode }, { "HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].Count", request.HotelReservations.HotelReservation.RoomStay.GuestCounts.GuestCount[1].Count.ToString() }, { "HotelReservations.HotelReservation.RoomStay.BasicPropertyInfo.HotelCode", request.HotelReservations.HotelReservation.RoomStay.BasicPropertyInfo.HotelCode }, { "HotelReservations.HotelReservation.ResGlobalInfo.RoomCount", request.HotelReservations.HotelReservation.ResGlobalInfo.RoomCount.ToString() }, { "HotelReservations.HotelReservation.ResGlobalInfo.MemberLevel", request.HotelReservations.HotelReservation.ResGlobalInfo.MemberLevel }, { "HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.Start", request.HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.Start }, { "HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.End", request.HotelReservations.HotelReservation.ResGlobalInfo.TimeSpan.End } }); // 发送POST请求 HttpClient client = new HttpClient(); HttpResponseMessage response = await client.PostAsync("https://example.com", formData); // 处理响应并返回结果 string responseBody = await response.Content.ReadAsStringAsync(); return Ok(responseBody); } ``` 请注意,上述代码仅供参考。您需要根据自己的实际情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值