本文介绍了如何通过c#代码验证节点的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨朋友们, 我需要验证一个xml文件。 我的要求是优惠券应该先到 MaturityDate 如果它没有以指定的顺序出现它的错误, 我怎样才能实现这一目标? xpath会起作用吗?或者任何其他更好的选择。 请帮助 var ProjectedIncomePosition = Xele.Descendants( ProjectedIncomePosition); foreach ( var 优惠券 ProjectedIncomePosition) { // XmlDocument xml = new XmlDocument(); // xml.LoadXml(coupon.ToString()); // int position = xml.SelectNodes(// Coupon [position()]); } < ProjectedIncomePosition > < ; KeyAcct i:nil = true / > < Mod1Code > STK < / Mod1Code > < ExchangeRate > 0 < / ExchangeRate > < CallDate i:nil = true / > < 优惠券 > 0.59 < /优惠券 > < IsExternal > false < / IsExternal > < MaturityDate / > < PreRefundedDate i:nil = true / > < EstimatedIncome > 6.28980 < / EstimatedIncome > < / ProjectedIncomePosition > 解决方案 尝试使用以下代码: string xmlData = @ < projectedincomeposition>< keyacct xmlns:h = true /><mod1code>STK</mod1code><exchangerate>0</exchangerate><coupon>0.59 < / coupon>< isexternal> false< / isexternal>< maturitydate />< / projectedincomeposition>; // 获取所有元素 IEnumerable< XElement> roots = XElement.Parse(xmlData).Elements(); // 优惠券索引 int coupon = roots。选择((item,index)= > new {elementName = item.Name,Index = index})。其中(p = > p.elementName == 优惠券)。选择(p = > p.Index + 1 )FirstOrDefault(); // 到期日期指数 int maturityDate = roots.Select((item,index)= > new {elementName = item.Name,Index = index})。其中(p = > p.elementName == MaturityDate)。选择(p = > p.Index + 1 )FirstOrDefault(); if (优惠券< maturityDate) { // 正确 } else { // 数据不正确 } 您要做的只是.xsd xml架构定义的角色,应该以这种方式处理。 定义模式将允许您清楚明确地定义xml文件的有效内容(包括标签,允许的多少内容及其各自的订单),以及以编程方式在架构中再次验证特定的xml文件。 一些链接: XML Schema(W3C)(维基百科) [ ^ ] W3C XML架构定义语言(XSD)1.1 [ ^ ] XML图式教程(w3schools) [ ^ ] Hi friends,I need to validate an xml file. My requirement is Coupon should come first then MaturityDate if its not coming in the specified order its an error,How can i achieve this? will the xpath works? or any other better alternative.Please help var ProjectedIncomePosition = Xele.Descendants("ProjectedIncomePosition"); foreach(var coupon in ProjectedIncomePosition) { //XmlDocument xml = new XmlDocument(); //xml.LoadXml(coupon.ToString()); //int position =xml.SelectNodes("//Coupon[position()]"); }<ProjectedIncomePosition><KeyAcct i:nil="true"/><Mod1Code>STK</Mod1Code><ExchangeRate>0</ExchangeRate><CallDate i:nil="true"/><Coupon>0.59</Coupon><IsExternal>false</IsExternal><MaturityDate/><PreRefundedDate i:nil="true"/><EstimatedIncome>6.28980</EstimatedIncome></ProjectedIncomePosition> 解决方案 Try with below code:string xmlData = @"<projectedincomeposition><keyacct xmlns:h="true" /><mod1code>STK</mod1code><exchangerate>0</exchangerate><coupon>0.59</coupon><isexternal>false</isexternal><maturitydate /></projectedincomeposition>";// Getting all elementsIEnumerable<XElement> roots = XElement.Parse(xmlData).Elements();// Coupon indexint coupon = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "Coupon").Select(p => p.Index + 1).FirstOrDefault();// Maturity Date Indexint maturityDate = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "MaturityDate").Select(p => p.Index + 1).FirstOrDefault();if (coupon < maturityDate){// Correct}else{// Incorrect data}What you are trying to do is purely the role of a .xsd xml schema definition, and should be handled this way.Defining a schema would allow you to clearly and definitely define the valid content of your xml file (including tags, how much of them are allowed, and their respective orders), and to programmatically validate a specific xml file againt the schema.A few links:XML Schema (W3C) (Wikipedia)[^]W3C XML Schema Definition Language (XSD) 1.1[^]XML Schema Tutorial (w3schools)[^] 这篇关于如何通过c#代码验证节点的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 12:11