XML Heeeeeeeelp !

R

rene.rugerio

hi, i am facing this trouble (pretty obvious tho')
im working with a xmldocument, the trick is this is coming with some
empty attributes
i want to browse for the empty attributes and to get rid of them
this is the example of what do i receive
and under is what ive tried
it does only displays the name of the empty attribute
but do not know how to erase that from the node

SO MANY THANKS IN ADVANCE, PEOPLE !!!

===========================================================
NOW IS LIKE :
example attrib1="" attrib2="yes" attrib3="no" attrib4=""
node attribX=""
-----------------------------------------------------------
SHOULD BE LIKE :
example attrib2="yes" attrib3="no"
node
===========================================================


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("nuevo.xml");
XmlNodeList nodes = xmlDoc.SelectNodes("//@*");
foreach(XmlNode node in nodes)
{
if (node.Value.ToString() == "")
{
Console.WriteLine(node.Name.ToString());
}
}
Console.ReadLine();
 
D

Danny Tuppeny

hi, i am facing this trouble (pretty obvious tho')
im working with a xmldocument, the trick is this is coming with some
empty attributes
i want to browse for the empty attributes and to get rid of them
this is the example of what do i receive
and under is what ive tried
it does only displays the name of the empty attribute
but do not know how to erase that from the node

What about:

node.ParentNode.RemoveChild(node) ?
 
S

Sebastian@IG&A::

The really quick solution I can think off is to rewrite the xml
document and not including those attributes you don't want.
I assume you've already tryied to delete them.

Good luck!
 
K

Kai Brinkmann [MSFT]

I don't think that would work. The ParentNode property returns null for
nodes of type XmlNodeType.Attribute.

You may have to retrieve all element nodes with attributes and then
specifically delete blank attributes. For example:

ArrayList alRemoveNodes = new ArrayList();
foreach (XmlNode node in xmlDoc.SelectNodes("//*[@*]"))
{
alRemoveNodes.Clear();

foreach (XmlNode attrNode in node.Attributes)
{
if (attrNode.InnerText.Equals(String.Empty))
alRemoveNodes.Add(attrNode);
}

foreach (XmlNode attrNode in RemovesNodes)
{
node.Attributes.Remove(attrNode);
}
}

The example uses two inner loops, because you cannot modify the collection
used to drive the foreach loop inside the loop itself. So you have to first
collect the attributes you want to delete and then remove them in a separate
loop.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Danny Tuppeny

Kai Brinkmann said:
I don't think that would work. The ParentNode property returns null for
nodes of type XmlNodeType.Attribute.

Whoops! Shows how much attention I was paying to the original post - I
didn't realise we were removing attributes, not nodes! :)
 
T

Truong Hong Thi

The following code should be fine:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("nuevo.xml");

XmlNodeList emptyAttrs = xmlDoc.SelectNodes("//@*[.= '']");
foreach(XmlAttribute attr in emptyAttrs)
{
attr.OwnerElement.RemoveAttributeNode(attr);
}

Regards,
Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top