![](/img/trans.png)
[英]How to get InnerText and InnerXml of a Node from XmlDocument?
[英]How to get the InnerXml of matching text
我正在使用C#。
我有一个带有InnerXml
的XmlElement
<b xpage="5" xid="85">3-6. Title</b><i>. The Content</i>
这使得InnerText
为
3-6。 标题
现在我有一个具有值的匹配变量
3-6. Title.
我想要得到的是InnerXml ..ie的其余部分。
<i> The Content</i>
请帮忙。
在OuterXml属性中,您将获得所需的xml字符串<i>. The Content</i>
<i>. The Content</i>
XmlDocument doc = new XmlDocument();
doc.LoadXml("<rootxml><b xpage=\"5\" xid=\"85\">3-6. Title</b><i>. The Content</i></rootxml>");
XmlNode root = doc.FirstChild;
var completeString = root["b"].OuterXml + root["i"].OuterXml;
if (completeString.StartsWith("3-6. Title.") {
XmlElement xmlElement = root["i"];
var iContent = xmlElement.OuterXml;
Console.WriteLine(iContent);
}
希望这可以帮助
看来您有两个兄弟节点B
和I
要匹配B
的特定实例,请使用XPath文本选择器。
string xpath = "//*/b[text()='3-6. Title']"; // replace wildcard with real path
http://publib.boulder.ibm.com/infocenter/iseries/v6r1m0/index.jsp?topic=/db2/rbafzxpathqueryexmp.htm
匹配B
,可以使用NextSibling
属性找到I
http://msdn.microsoft.com/zh-CN/library/system.xml.xmlnode.nextsibling.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.