Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add attributes to the parent node of a type text node

Status
Not open for further replies.

jambalapamba

Programmer
May 26, 2008
1
EU
Hi

I am walking xml using c# and i want add attribute (style="text-decoration: line-through;" ) to the parent node of text node if the value meets some condition.

e.g if this element <div >include</div> as include doesn't contain "read" i want <div style="text-decoration: line-through;">include</div>

Here is wht i am doing

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmldoc);
XmlNodeReader reader = new XmlNodeReader(xDoc);
while (!reader.EOF)
{

reader.Read();
XmlNodeType nodeType = reader.NodeType;

Console.WriteLine(reader.Value);
if (nodeType == XmlNodeType.Element)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
//do something
}

}
else if (nodeType == XmlNodeType.Text)
{
if (reader.Value.ToString().Contains("read"))
{
//do something
}
else
{
//i want to add attribute to the parent element of this text node
}
}

} //end while



Can any one give me suggestiongs how to do it



Thank you

 
[0] You can check out this WriteShadowNode idea where they use XmlReader rather than XmlNodeReader, but it shouldn't be an unsurmountable barrier.

[1] However, your requirement is slightly different from the demo there. However, since you need to check the text node and if it satisfies some condition, add an attribute to the parent element. This is doable, replacing the condition at the read.Read() loop by something like this.
[tt]
while (reader.Read())
{
if (reader.NodeType==XmlNodeType.Text && !reader.Value.ToString().Contains("read"))
{
//here write is still ready to write to parent element
writer.WriteAttributeString("style","text-decoration:line-through;");
}
//here it is ready to do copy identical contents
WriteShallowNode(reader, writer);
}
[/tt]
Does it logic proper? The above may be quasi-pseudo-code only. You might have to do some refinement work on the condition.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top