这是为了获取"车辆标签"
以外的标签 document.Descendants(s0 + "Vehicles").Elements().ToList().ForEach(x => x.Add(document.Elements().Where(d => d.Element != s0 + "Vehicles")));
需要从基础xml添加除车辆之外的元素,并尝试将其另存为路径中的新xml文件。
Base 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运营商&#39;!=&#39;不能应用于类型&#39;方法组的操作数&#39;和&#39; System.Xml.Linq.XName&#39; c:\ users \ 502556385 \ documents \ visual studio 2012 \ Projects \ SplitXML \ SplitXML \ Program.cs 31 122 SplitXML
答案 0 :(得分:1)
您需要使用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
来获取字符串名称。