使用C#编辑XML文档 [英] Editing XML document using C#
问题描述
我在解决如何向XML文档中添加元素时遇到了一些麻烦, 我想将热点信息添加到ID正确的xml中(因此,在id = 2处添加热点信息),这是我当前的XML-
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>
我希望更新xml,以便显示类似这样的内容-
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是我想与XML文档匹配以在何处添加热点的ID-Page.Value.IndexOf(-")返回xml中页面的ID)
(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)
但这只是将代码添加到页面的底部,因此我需要找到一种方法将其添加到XML的正确ID所在的Hotspots部分中.
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.
任何帮助将不胜感激,如果有更好的方法来执行我正在尝试的操作,请告诉我,我以前从未在代码中实际使用过XML文档,并且最近才开始学习c#(在上个月内) ).
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).
谢谢.
推荐答案
选择所需的页面
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");
}
您的代码将新的Hotspot元素添加到页面,而不是将内容添加到现有的Hotspots元素.
Your code adds new Hotspot element to page, instead of adding content to existing Hotspots element.
这篇关于使用C#编辑XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!