拆分xml文件并保留父节点信息

发布于 2024-10-14 23:15:13 字数 1912 浏览 2 评论 0原文

无论如何,通过使用 LINQ、XmlDocument 等,我可以将一个大的 xml 文件分割成较小的块并保留所有父节点值

对于在原始文件中找到的每个单元节点,示例 XML

<Inventory>
   <Dealer>
       <ID>123</ID>
       <Phone>1235551234</Phone>
       <Units>
          <Unit>
             <Year>
             <Make />
             <Model />
          </Unit>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
      </Units>
  </Dealer>
   <Dealer>
       <ID>124</ID>
       <Phone>1235554321</Phone>
       <Units>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
      </Units>
  </Dealer>
</Inventory>

块应该如下所示

<Inventory>
   <Dealer>
       <ID>123</ID>
       <Phone>1235551234</Phone>
       <Units>
          <Unit>
             <Year>
             <Make />
             <Model />
          </Unit>
       </Units>
  </Dealer>
</Inventory>

我应该澄清一个问题,我尝试以编程方式构建从“Unit”节点开始的块。不知道父元素中可能包含什么。

到目前为止,我的解决方案是,

XDocument document = XDocument.Load("sample.xml");
var units = document.Descendents("Unit").ToList();
foreach (XElement unit in units) 
{
    XElement parent = unit;
    XElement child  = parent;
    while (null != parent.Parent) 
    {
        parent = parent.Parent;
        parent.Descendents(child.Name).Remove();
        parent.Add(child);
        child = parent;
    }
} 

这对于每个经销商节点中的第一个单元非常有效,该经销商的每个后续单元都会丢失任何父信息,并且该块仅包含单元节点。

Is there anyway, either by using LINQ, XmlDocument etc, that I can split a large xml file into smaller chucks and retain all the parent node values

Sample XML

<Inventory>
   <Dealer>
       <ID>123</ID>
       <Phone>1235551234</Phone>
       <Units>
          <Unit>
             <Year>
             <Make />
             <Model />
          </Unit>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
      </Units>
  </Dealer>
   <Dealer>
       <ID>124</ID>
       <Phone>1235554321</Phone>
       <Units>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
          <Unit>
             <Year />
             <Make />
             <Model />
          </Unit>
      </Units>
  </Dealer>
</Inventory>

Chunks should look like this for each Unit node found in the original

<Inventory>
   <Dealer>
       <ID>123</ID>
       <Phone>1235551234</Phone>
       <Units>
          <Unit>
             <Year>
             <Make />
             <Model />
          </Unit>
       </Units>
  </Dealer>
</Inventory>

I should clarify one issue, I'm trying to programmaticly build the chunks starting with the "Unit" node. w/o having knowledge of what may be contained in the parent elements.

My solution so far is

XDocument document = XDocument.Load("sample.xml");
var units = document.Descendents("Unit").ToList();
foreach (XElement unit in units) 
{
    XElement parent = unit;
    XElement child  = parent;
    while (null != parent.Parent) 
    {
        parent = parent.Parent;
        parent.Descendents(child.Name).Remove();
        parent.Add(child);
        child = parent;
    }
} 

This works great for the first unit in each dealer node, each subsequent unit for that dealer loses any parent information and the chunk only contains the Unit node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2024-10-21 23:15:14

使用 selectSingleNode 或 selectNodes 方法选择节点并将其从 xml 文档中删除。

Select a node(s) using selectSingleNode or selectNodes method and remove it from xml document.

╰◇生如夏花灿烂 2024-10-21 23:15:13

您可以使用 LINQ to XML + LINQ to Objects 来完成此操作,为了清楚起见,我将解决方案分为三个部分:首先选择单个单元,然后选择突变,然后分块:

 //load test xml
XDocument doc = XDocument.Load(@"test.xml");

//select
var units = doc.Descendants("Unit")
               .Select(unit => new { Unit = unit, Dealer = unit.Parent.Parent })
               .ToList();

//mutate
units.ForEach(unit => 
              { 
                  unit.Dealer.Descendants("Unit").Remove(); 
                  unit.Dealer.Element("Units").Add(unit.Unit); 
              });

//Split into chunks
var chunks = units.Select( unit => new XDocument(new XElement("Inventory", unit.Dealer)))
                  .ToList();

You can do this with LINQ to XML + LINQ to Objects, for clarity I've split the solution into three parts: first selecting the individual units, then the mutation, then the chunking:

 //load test xml
XDocument doc = XDocument.Load(@"test.xml");

//select
var units = doc.Descendants("Unit")
               .Select(unit => new { Unit = unit, Dealer = unit.Parent.Parent })
               .ToList();

//mutate
units.ForEach(unit => 
              { 
                  unit.Dealer.Descendants("Unit").Remove(); 
                  unit.Dealer.Element("Units").Add(unit.Unit); 
              });

//Split into chunks
var chunks = units.Select( unit => new XDocument(new XElement("Inventory", unit.Dealer)))
                  .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文