I want to split xml file by tags with attributes into multiple files.
Here is my xml
:
Source XML document
<BATCH ID="131070" Date_Submitted="12/1/2014 7:36:06 AM" Date_Received="12/1/2014 7:36:06 AM" Date_Processed="12/1/2014 7:40:00 AM" UserID="PAYPINST" Error_Count="1">
<CLAIMS Submitter="541884924" Submitter_Num="" Version="005010X223A2">
<CLAIM Rec_ID="1" Claim_Type="Institutional" Submitter="541884924">
</CLAIM>
<CLAIM Rec_ID="2" Claim_Type="Institutional" Submitter="541884924">
</CLAIM>
</CLAIMS>
</BATCH>
Here I want to split the file by CLAIM
. I have used following code.
XDocument doc = XDocument.Load(xmlFilePath);
var newDocs = doc.Descendants("CLAIM")
.Select(d => new XDocument(new XElement("BATCH", new XElement("CLAIMS", d))));
foreach (var newDoc in newDocs)
{
newDoc.Save(SplitedxmlFileName);
}
When I run the code it gives me splited file. But it doesn't give me the attributes of tag.
I refer this link :How to Split an XML file into multiple XML Files
I want output something like this:
Should split to two xml documents as below
1)
<BATCH ID="131070" Date_Submitted="12/1/2014 7:36:06 AM" Date_Received="12/1/2014 7:36:06 AM" Date_Processed="12/1/2014 7:40:00 AM" UserID="PAYPINST" Error_Count="1">
<CLAIMS Submitter="541884924" Submitter_Num="" Version="005010X223A2">
<CLAIM Rec_ID="1" Claim_Type="Institutional" Submitter="54188424">
</CLAIM>
</CLAIMS>
</BATCH>
2)
<BATCH ID="131070" Date_Submitted="12/1/2014 7:36:06 AM" Date_Received="12/1/2014 7:36:06 AM" Date_Processed="12/1/2014 7:40:00 AM" UserID="PAYPINST" Error_Count="1">
<CLAIMS Submitter="541884924" Submitter_Num="" Version="005010X223A2">
<CLAIM Rec_ID="2" Claim_Type="Institutional" Submitter="51884924">
</CLAIM>
</CLAIMS>
</BATCH>
How can I split the xml file by tags with attributes?