【问题标题】:Editing XML document using C#使用 C# 编辑 XML 文档
【发布时间】:2013-07-01 14:33:58
【问题描述】:

我在弄清楚如何将元素添加到我的 XML 文档中时遇到了一些麻烦, 我想将热点信息添加到 Id 正确的 xml 中(所以 id=2 添加热点信息)这是我当前的 XML -

  <Pages>
    <Page>
      <Id>1</Id>
      <Title>TEST</Title>
      <ContentUrl>Images\testimg.png</ContentUrl>
      <Hotspots>
        <Hotspot>
          <X>140</X>
          <Y>202</Y>
          <Shape>Circle</Shape>
          <TargetId>2</TargetId>
        </Hotspot>
      </Hotspots>
      <ParentId>0</ParentId>
    </Page>
    <Page>
      <Id>2</Id>
      <Title>TEST2</Title>
      <ContentUrl>Images\testimg2.jpg</ContentUrl>
      <Hotspots>
      </Hotspots>
      <ParentId>1</ParentId>
    </Page>
</Pages>

我希望更新 xml,使其显示类似这样的内容 -

<Pages>
        <Page>
          <Id>1</Id>
          <Title>TEST</Title>
          <ContentUrl>Images\testimg.png</ContentUrl>
          <Hotspots>
            <Hotspot>
              <X>140</X>
              <Y>202</Y>
              <Shape>Circle</Shape>
              <TargetId>2</TargetId>
            </Hotspot>
          </Hotspots>
          <ParentId>0</ParentId>
        </Page>
        <Page>
          <Id>2</Id>
          <Title>TEST2</Title>
          <ContentUrl>Images\testimg2.jpg</ContentUrl>
          <Hotspots>
            <Hotspot>
              <X>140</X>
              <Y>202</Y>
              <Shape>Circle</Shape>
              <TargetId>2</TargetId>
            </Hotspot>
          </Hotspots>
          <ParentId>1</ParentId>
        </Page>

我现在的代码是-

XDocument Xdoc = XDocument.Load(@"Test.xml");
    Xdoc.Root.Element("Pages").Elements("Page").Where(Page => Page.Value.Substring(0,Page.Value.IndexOf("-"))==CurrentPage.Id.ToString())
    .FirstOrDefault()
    .Add(new XElement("Hotspot",
                       new XElement("X", x), 
                       new XElement("Y", y),
                       new XElement("Shape", "Circle"),
                       new XElement("TargetId", nNodeID)
                    ));
    Xdoc.Save(@"Test.xml");

(CurrentPage.Id 是我想与 XML 文档匹配的 id,用于添加热点的位置 - Page.Value.IndexOf("-") 返回 xml 中页面的 ID)

但这只是将代码添加到页面底部,因此我需要找到一种方法将其添加到正确 Id 所在的 XML 的热点部分。

任何帮助将不胜感激,如果有更好的方法来做我正在尝试的事情,请告诉我,我之前从未在我的代码中实际使用过 XML 文档,并且最近才开始学习 c#(上个月)。

谢谢。

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    选择你需要的页面

    XDocument xdoc = XDocument.Load("Test.xml");
    int pageId = 2;
    var page = xdoc.Descendants("Page")
                    .FirstOrDefault(p => (int)p.Element("Id") == pageId);
    

    然后将内容添加到这个页面元素(如果有的话):

    if (page != null)
    {
        // add to Hotspots element
        page.Element("Hotspots")
            .Add(new XElement("Hotspot",
                     new XElement("X", x),
                     new XElement("Y", y),
                     new XElement("Shape", "Circle"),
                     new XElement("TargetId", nNodeID)));
    
        xdoc.Save("Test.xml");
    }
    

    您的代码向页面添加了新的 Hotspot 元素,而不是向现有的 Hotspots 元素添加内容。

    【讨论】:

    • 效果很好,只需要稍作调整即可按我的意愿工作 - .Add(new XElement("Hotspot" 就在添加 x 元素之前
    • @Markjw2 是的,很抱歉错过了那件事
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多