I have an XML document like this:
<?xml version='1.0' encoding='UTF-8'?>
<GateDocument>
<!-- The document's features-->
<GateDocumentFeatures>
<Feature>
<Name className="java.lang.String">MimeType</Name>
<Value className="java.lang.String">text/html</Value>
</Feature>
</GateDocumentFeatures>
<!-- The document content area with serialized nodes -->
<TextWithNodes><Node id="0" />astralis<Node id="8" /> <Node id="9" />ltd<Node id="12" />
<Node id="14" />{<Node id="15" />DOCUMENT<Node id="23" />}<Node id="24" /> <Node id="25" />{<Node id="26" />TYPE<Node id="30" />}<Node id="31" />EX-<Node id="34" />10<Node id="36" />.<Node id="37" />12<Node id="39" /> <Node id="40" />{<Node id="41" />SEQUENCE<Node id="49" />}<Node id="50" />3<Node id="51" /> <Node id="52" />{<Node id="53" />FILENAME<Node id="61" />}<Node id="62" />e<Node id="63" />300201<Node id="69" />_<Node id="70" />ex<Node id="72" />10<Node id="74" />-<Node id="75" />12<Node id="77" />.<Node id="78" />txt<Node id="81" /> <Node id="82" />{<Node id="83" />DESCRIPTION<Node id="94" />}<Node id="95" />
</TextWithNodes>
<Annotations>
.
.
.
.
.
</Annotations>
<Annotations Name="Original markups">
.
.
.
.
</Annotations>
</GateDocument>
I have loaded this XML in XMLDocument using vb.net/asp.net. I need data between Nodes with id > 16 and Id < 30 that is “}{TYPe}” Please note that It is not attribute’s value. It is data between nodes. I created following code but It is very slow.
Dim sb As New StringBuilder()
Dim document As New XPathDocument(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/gate_xml_output.xml"))
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim iterator As XPathNodeIterator = navigator.Select("//TextWithNodes/node()[preceding-sibling::Node[@id=" & startNode & "] and following-sibling::Node[@id=" & endNode & "]]")
While iterator.MoveNext()
sb.Append(iterator.Current.Value & " ")
End While
May be I need to modify following code:
XDocument doc = XDocument.Parse(@"<TextWithNodes>...</TextWithNodes");
string result = string.Join(" ",
doc.Root
.Nodes()
.SkipWhile(n => n.NodeType != XmlNodeType.Element ||
(int)((XElement)n).Attribute("id") != 16)
.TakeWhile(n => n.NodeType != XmlNodeType.Element ||
(int)((XElement)n).Attribute("id") != 30)
.OfType<XText>());
// result == "pet DOCUMENT"
What I need to supply to XDocument.Parse.
Please suggest.
I used the range 15 to 30
This returns the elements. You can get the contents with ToString()