获取和修改 xml 文档中的子元素 [英] Fetching and modifying the child elements in an xml Document
问题描述
这是一个棘手的情况.我有一个返回 XML 文档的 Web 服务.此 XML 文档有一个名为
的子元素.我需要修改文章的子元素之一,并将每个文章元素放置"回网络服务.我不能用实体发布整个文档的原因是因为 Web 服务无法将实体识别为对象,我无法对其执行更新操作.
Here is a tricky bit of situation. I have a webservice that returns an XML document. This XML document has a root element that is called <Entities .... >
The Entities element has child elements called <Article..>
. I need to modify one of the child elements of Article and "PUT" each article element back to the webservice. The reason why I can not post the whole document with Entities is because the webservice does not recognize the Entities as an object and I can not perform an update operation on it.
以下是收到的文件的结构:
Below is the structure of the document received:
<Entities>
<Article id="1">
<Permissions>
<Sla id="1">
<name> first sla </name>
</Sla>
</Permissions>
</Article>
<Article id="2">
<Permissions>
<Sla id="2">
<name> second sla </name>
</Sla>
</Permissions>
</Article>
</Entities>
下面是我用来完成这个技巧的代码,但我可以获取 Sla 元素.我需要做的是获取每篇文章中的 Sla 元素并对其 id 属性进行检查.如果检查为真,我需要删除该 Sla 元素及其所有子元素.这是我到目前为止所做的:
Below is the code that I have used to do the trick but I can fetch the Sla element. What I need to do is to get the Sla elements in each article and run a check against its id attribute. If the check comes true, I need to remove that Sla element along with all of its child elements. This is what I have done so far:
int pageNumber = 1;
bool entities = true;
while (entities)
{
url = "www.myurl.com";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "Get";
HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
doc.Load(rep.GetResponseStream());
rep.Close();
if (doc != null)
{
XmlNodeList nodes = doc.SelectNodes("/Entities/Article");
foreach (XmlNode node in nodes)
{
XmlNode Slanode = null;
try
{
Slanode = doc.SelectSingleNode("Permissions/Sla[@id='" + sla.ToString() + "']");
Slanode.ParentNode.RemoveChild(node);
string finalXML = doc.OuterXml;
HttpWebRequest reqToUpdate = (HttpWebRequest)WebRequest.Create(url);
reqToUpdate.ContentType = "text/xml; encoding=UTF-8";
reqToUpdate.Method = "PUT";
byte[] bytes = new UTF8Encoding().GetBytes(finalXML);
reqToUpdate.ContentLength = bytes.Length;
Stream data = reqToUpdate.GetRequestStream();
data.Write(bytes, 0, bytes.Length);
data.Close();
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
pageNumber++;
}
else
entities = false;
}
}
我无法让代码工作,因为当我到达
I cant get the code to work because when I reach the
Slanode = doc.SelectSingleNode("Permissions/Sla[@id='" + sla.ToString() + "']");
它不填充 Slanode 并返回 null,当我在调试模式下检查节点时,列表显示以下错误:
it does not populate the Slanode and it returns null, when I checked the nodes in debug mode, the list was showing the following error:
一个 XML 文档中只允许有一个顶级元素.错误处理资源
任何帮助将不胜感激.
谢谢
推荐答案
我打算用 XElement 重写它,希望这对你有用,因为 XElement 就是我所理解的.
I'm going to re-write that with XElement, hopefully that works for you as XElement is what I understand.
首先获取要删除的文章:
First get the Article to remove:
XElement root = XElement.Load(stream);
XElement articleToRemove = root.XPathSelectElement("//Article[Permissions/Sla/@id='"+sla.ToString()+"']");
if(null != articleToRemove)
articleToRemove.Remove();
为了使循环可读,创建一个 Put 函数:
To make the loop readable, create a Put function:
private void Put(XElement article)
{
Stream data = null;
try
{
string finalXML = article.ToString(SaveOptions.DisableFormatting);
HttpWebRequest reqToUpdate = (HttpWebRequest)WebRequest.Create(url);
reqToUpdate.ContentType = "text/xml; encoding=UTF-8";
reqToUpdate.Method = "PUT";
byte[] bytes = new UTF8Encoding().GetBytes(finalXML);
reqToUpdate.ContentLength = bytes.Length;
data = reqToUpdate.GetRequestStream();
data.Write(bytes, 0, bytes.Length);
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
finally
{
if (null != data)
data.Close();
}
}
然后获取其余文章并上传:
Then get the rest of the articles and upload them:
root.Descendants("Article").ToList().ForEach(a => Put(a));
要将 XPath 与 XElement 一起使用,您需要使用以下内容:
To use XPath with XElement you need to use the following using:
using System.Xml.XPath;
这篇关于获取和修改 xml 文档中的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!