简体   繁体   English

使用C#中的标签属性按标签拆分xml

[英]Split xml by tag with tag attributes in c#

I want to split xml file by tags with attributes into multiple files. 我想通过具有属性的标签将xml文件拆分为多个文件。

Here is my xml : 这是我的xml

Source XML document 源XML文档

<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 . 在这里我想通过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 我引用此链接: 如何将一个XML文件拆分为多个XML文件

I want output something like this: 我想输出这样的东西:

Should split to two xml documents as below 应该拆分为两个xml文档,如下所示

1) 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) 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 ? 如何通过带有属性标签拆分xml文件?

I have tried to segregate the steps. 我试图隔离步骤。 You were able to distinguish between different nodes. 您能够区分不同的节点。 So you just need the Parent Node Attributes. 因此,您只需要父节点属性。 Assuming that the attribute values of BATCH & CLAIMS will always be same for each and every xml file. 假设BATCHCLAIMS的属性值对于每个xml文件始终是相同的。 You can try the below modification of your code. 您可以尝试对代码进行以下修改。

XDocument doc = XDocument.Load("XMLFile1.xml");
var newDocs = doc.Descendants("CLAIM")
                         .Select(d => new XDocument(new XElement("BATCH", new XElement("CLAIMS", d))));

var batch = doc.Element("BATCH");
var claims = doc.Descendants("CLAIMS");

int i = 0;
foreach (var newDoc in newDocs)
{
    foreach (XAttribute xat in batch.Attributes())
    {
        newDoc.Element("BATCH").SetAttributeValue(xat.Name, xat.Value);
    }
    foreach (XElement claim in claims)
    {
        foreach (XAttribute xat in claim.Attributes())
        {
            newDoc.Descendants("CLAIMS").ElementAt(0).SetAttributeValue(xat.Name, xat.Value);
        }
    }

    newDoc.Save(i.ToString());
    ++i;
}

Let me know, if it's of any help. 让我知道是否有帮助。

EDIT 编辑

XDocument doc = XDocument.Load("XMLFile1.xml");
var newDocs = doc.Descendants("CLAIM")
                         .Select(d => new XDocument(new XElement("BATCH", new XElement("CLAIMS", d))));

var batch = doc.Element("BATCH");
var claims = doc.Descendants("CLAIMS");

int i = 0;
foreach (var newDoc in newDocs)
{
    batch.Attributes().All(p => { newDoc.Element("BATCH").SetAttributeValue(p.Name, p.Value); return true; });

    claims.All(p => p.Attributes().All(k => { newDoc.Descendants("CLAIMS").ElementAt(0).SetAttributeValue(k.Name, k.Value); return true; }));

    newDoc.Save(i.ToString());
    ++i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM