Editing XML document using C#

2019-06-28 08:19发布

问题:

I have been having some trouble working out how to add elements into my XML document, I am wanting to add hotspot information into the xml where the Id is correct (so where id=2 add hotspot information) this is my current 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>

I want the xml to be updated so it shows something like this -

<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>

The code i have up to now is -

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 is the id i want matched with the XML document for where to add the Hotspot - Page.Value.IndexOf("-") returns the Id of the page within the xml)

but this just adds the code at the bottom of a page, so i need to find a way to add it into the Hotspots section of the XML where the correct Id is.

Any help would be appreciated and if there is a better way of doing what i am trying please let me know, i have never actually worked with XML documents within my code before and have only recently started learning c# (within the last month).

thanks.

回答1:

Select page which you need

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

And then add content to this page element (if any):

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");
}

Your code adds new Hotspot element to page, instead of adding content to existing Hotspots element.