如何应用不等于的Linq XName
本文关键字:不等于 Linq XName 应用 何应用 | 更新日期: 2023-09-27 18:28:34
这样做是为了获取除"车辆标签"之外的标签
document.Descendants(s0 + "Vehicles").Elements().ToList().ForEach(x => x.Add(document.Elements().Where(d => d.Element != s0 + "Vehicles")));
需要从基本xml中添加车辆以外的元素,并尝试将其保存为路径中的新xml文件。
基本Xml是
<?xml version="1.0" encoding="utf-8"?>
<Policy >
<PolicyNumber></PolicyNumber>
<Tracking>
<ID></ID>
<LocationID></LocationID>
<LocationName>Wheat Ridge</LocationName>
<LocationPhoneNumber>3034230162</LocationPhoneNumber>
<LocationFaxNumber></LocationFaxNumber>
<LocationEmailAddress></LocationEmailAddress>
</Tracking>
<CurrentTerm>
<MailingAddress>
<Type>Mailing</Type>
<AddressLine>PO BOX 12</AddressLine>
<City>LONGMONT</City>
<State>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</State>
<ZipCode>805</ZipCode>
</MailingAddress>
<RiskState>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</RiskState>
<EffectiveDate>2015-10-01T00:00:00</EffectiveDate>
<ExpirationDate>2016-10-01T00:00:00</ExpirationDate>
<Vehicles>
<Vehicle>
<Number>1</Number>
<Year>1997</Year>
<Make>FORD</Make>
<Model>F-250 HEAVY DUTY</Model>
</Vehicle>
<Vehicle>
<Number>3</Number>
<GenerateIDCard>Y</GenerateIDCard>
<Year>2012</Year>
<Make>TOYOTA</Make>
<Model>CAMRY L/LE/SE/XLE</Model>
</Vehicle>
</Vehicles>
</CurrentTerm>
</Policy>
输出应该是两个文件
文件1:
<?xml version="1.0" encoding="utf-8"?>
<Policy >
<PolicyNumber></PolicyNumber>
<Tracking>
<ID>1249</ID>
<LocationID>2001</LocationID>
<LocationName>Wheat Ridge</LocationName>
<LocationPhoneNumber>3034230162</LocationPhoneNumber>
<LocationFaxNumber></LocationFaxNumber>
<LocationEmailAddress></LocationEmailAddress>
</Tracking>
<CurrentTerm>
<MailingAddress>
<Type>Mailing</Type>
<AddressLine>PO BOX 12</AddressLine>
<City>LONGMONT</City>
<State>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</State>
<ZipCode>805</ZipCode>
</MailingAddress>
<RiskState>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</RiskState>
<EffectiveDate>2015-10-01T00:00:00</EffectiveDate>
<ExpirationDate>2016-10-01T00:00:00</ExpirationDate>
<Vehicle>
<Number>1</Number>
<Year>1997</Year>
<Make>FORD</Make>
<Model>F-250 HEAVY DUTY</Model>
</Vehicle>
</CurrentTerm>
</Policy>
文件2:
<?xml version="1.0" encoding="utf-8"?>
<Policy >
<PolicyNumber></PolicyNumber>
<Tracking>
<ID>1249</ID>
<LocationID>2001</LocationID>
<LocationName>Wheat Ridge</LocationName>
<LocationPhoneNumber>3034230162</LocationPhoneNumber>
<LocationFaxNumber></LocationFaxNumber>
<LocationEmailAddress></LocationEmailAddress>
</Tracking>
<CurrentTerm>
<MailingAddress>
<Type>Mailing</Type>
<AddressLine>PO BOX 12</AddressLine>
<City>LONGMONT</City>
<State>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</State>
<ZipCode>805</ZipCode>
</MailingAddress>
<RiskState>
<ISONumber>05</ISONumber>
<PostalCode>CO</PostalCode>
<StateName>Colorado</StateName>
</RiskState>
<EffectiveDate>2015-10-01T00:00:00</EffectiveDate>
<ExpirationDate>2016-10-01T00:00:00</ExpirationDate>
<Vehicle>
<Number>3</Number>
<VehicleIdentificationNumber>4T1BF1FK3CU526945</VehicleIdentificationNumber>
<Year>2012</Year>
<Make>TOYOTA</Make>
<Model>CAMRY L/LE/SE/XLE</Model>
</Vehicle>
</CurrentTerm>
</Policy>
但是我犯了一个我们不能用的错误操作人员错误2运算符"!="无法应用于"方法组"answers"System.Xml.Linq.XName"类型的操作数c:''users''502556385''documents''visual studio 2012''Projects''SplitXML''SplitXML''Program.cs 31 122 SplitXML
您需要使用XName
,而不是字符串:
document.Descendants(XName.Get("Vehicles",s0)).Elements().ToList()
.ForEach(x => x.Add(document.Elements().Where(d => d.Name != XName.Get("Vehicles",s0)));
该方法使用以下参数:XName.Get(string tagName,string namespace).
如果s0
确实是名称空间(XNameSpace
),则需要使用s0.NameSpace
来获取字符串名称。