Reputation: 97
<sg>
<userdata>
<data>
<tag name="gf" description="fg" nodeid=" {2F2CC6951E2B4EEA979F357164CB73E5}" controllerid="" keytype="" mask="" track="widthsegment" numkey="" interpolator="" frame="" aindex="1" number="1">
<![CDATA[ //sg/objects/object[@id=" {2F2CC6951E2B4EEA979F357164CB73E5}"]/params/param[@name="widthsegment"]
]]>
</tag>
<tag name="gf" description="fg" nodeid=" {2F2CC6951E2B4EEA979F357164CB73E5}" controllerid="" keytype="" mask="" track="widthsegment" numkey="" interpolator="" frame="" aindex="1" number="2">
<![CDATA[ //sg/objects/object[@id=" {2F2CC6951E2B4EEA979F357164CB73E5}"]/params/param[@name="widthsegment"]
]]>
</tag>
</data>
</userdata>
</sg>
I want to remove the " tag " - tag from this how to do this
I made like this
updatedData.SelectSingleNode("//tag[@name='" + 1 + "']").RemoveAll();
But still tag comes in this .. i mean empty tag .. how to remove that
after that
<sg>
<userdata>
<data>
<tag /> --- This is wat i want to remove... how to remove this
<tag name="gf" description="fg" nodeid=" {2F2CC6951E2B4EEA979F357164CB73E5}" controllerid="" keytype="" mask="" track="widthsegment" numkey="" interpolator="" frame="" aindex="1" number="2">
<![CDATA[ //sg/objects/object[@id=" {2F2CC6951E2B4EEA979F357164CB73E5}"]/params/param[@name="widthsegment"]
]]>
</tag>
</data>
Upvotes: 1
Views: 11568
Reputation: 97
XmlNodeList nodes = updatedData.GetElementsByTagName("tagname");
foreach (XmlNode node in nodes)
{
if (node.ChildNodes.Count == 0)
node.RemoveAll();
else
UpdateDoc.InnerXml = node.OuterXml;
}
I solved this ...
Upvotes: 2
Reputation: 6729
this is how you do the loading for xdocument to XmlNodeList
XDocument doc = XDocument.Load(@"C:\Users\sghaida\Documents\Visual Studio 2008\Projects\testing-ws-1\XMLParser\config.xml");
XmlDocument xmlDoc = new XmlDocument();
XmlNodeReader nodeReader = (XmlNodeReader)doc.CreateReader();
xmlDoc.ReadNode(nodeReader);
XmlNodeList xmlnodeList = xmlDoc.SelectNodes("node");
Upvotes: 0
Reputation: 498904
Your XPath is strange:
"//tag[@name='" + 1 + "']"
It will select all tag
elements that have a name
attribute with the value of 1. There are none in your example.
You are also using SelectSingleNode
which will only select a single node.
The XPath should be "//tag"
if you wish to select them all, and you should be using SelectNodes
instead.
Upvotes: 0
Reputation: 6729
var xmlDoc = XDocument.Load("filename.xml");
var element = (
from x in xmlDoc.Root.Elements("elemnt-name")
where x.Element("tage-name").Value == "xxxx"
select x
).FirstOrDefault();
element.Remove();
xmlDoc .Save("filename.xml");
Upvotes: 1