复制 xml 文件并在特定位置插入新元素 - C#

发布于 2024-08-31 07:48:55 字数 848 浏览 1 评论 0原文

您好,我想复制一个 xml 文件并在特定元素位置插入更多元素; 这样做的最好和最简单的方法是什么。我可以使用 xmlReader 读取元素并一一写入引用每种类型 - 我对此有一些问题,但除此之外,在我看来,工作量太大了,可以以某种方式做得更好。 在下面的示例中,我将 xml 作为默认定义,需要创建一个格式相同的新 xml,并将新值插入到sheet1 - 但在现有行之后,并对sheet2 执行相同的操作。

<book>
   <Sheet ss:name="Sheet1">
      <Table >
      <Row >
        <Cell/>
        <Cell>
             Title Name      
        </Cell>
        <Cell >
             Title Description
         </Cell>
      </Row>
   </Sheet>
<a/>
<b/>
  <Sheet ss:name="Sheet2">
      <Table >
      <Row >
        <Cell/>
        <Cell>
             Title Name      
        </Cell>
        <Cell >
             Title Description
         </Cell>
      </Row>
   </Sheet>
</book>

Hi I want to copy an xml file and insert in a specific element locaiton some more elements;
What is the best and easiest way doing this. I can use xmlReader read elements and write one by one referring to each and every type- I had some issues with this but besides this seems to me too mush work that can be done somehow better.
in the example below I have the xml as a default definition need to create a new xml in same format with inserting new values to sheet1 - but after existing rows, and do the same for sheet2.

<book>
   <Sheet ss:name="Sheet1">
      <Table >
      <Row >
        <Cell/>
        <Cell>
             Title Name      
        </Cell>
        <Cell >
             Title Description
         </Cell>
      </Row>
   </Sheet>
<a/>
<b/>
  <Sheet ss:name="Sheet2">
      <Table >
      <Row >
        <Cell/>
        <Cell>
             Title Name      
        </Cell>
        <Cell >
             Title Description
         </Cell>
      </Row>
   </Sheet>
</book>

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

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

发布评论

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

评论(1

榆西 2024-09-07 07:48:55

在我看来,最简单的方法是使用 LINQ to XML 加载整个文档,对其进行修改,然后再次保存。这可能比使用 XmlReader 更容易,根据我的经验,这可能会变得有些棘手。

然而,这确实涉及将其全部加载到内存中 - 如果文档很大,这可能会成为一个问题。这可能是个问题吗?

编辑:这是 LINQ to XML 中的一个简短示例(未经测试):

XDocument doc = XDocument.Load("test.xml");
XNamespace ss = "http://url/for/ss";
Sheet sheet1 = doc.Descendants("Sheet")
                  .Where(x => (string) x.Attribute(ss + "name") == "Sheet1");
XElement lastRow = sheet1.Elements("Row").LastOrDefault();
// Note: if there aren't any rows, lastRow will be null here. Handle accordingly
lastRow.AddAfterSelf(new XElement("Foo", "Extra value"));

如果您只想在工作表的所有旧内容之后添加新内容,则可以替代最后一部分:

sheet1.Add(new XElement("Foo", "Extra value"));

The simplest way in my view would be to load the whole document using LINQ to XML, modify it, then save it out again. That's likely to be easier than using XmlReader, which can get somewhat hairy in my experience.

However, that does involve loading it all into memory - which could be an issue if the documents are huge. Is that likely to be a problem?

EDIT: Here's a short example in LINQ to XML (untested):

XDocument doc = XDocument.Load("test.xml");
XNamespace ss = "http://url/for/ss";
Sheet sheet1 = doc.Descendants("Sheet")
                  .Where(x => (string) x.Attribute(ss + "name") == "Sheet1");
XElement lastRow = sheet1.Elements("Row").LastOrDefault();
// Note: if there aren't any rows, lastRow will be null here. Handle accordingly
lastRow.AddAfterSelf(new XElement("Foo", "Extra value"));

An alternative to the last part, if you just want the new content after all the old content of the sheet:

sheet1.Add(new XElement("Foo", "Extra value"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文