473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XML Heeeeeeeelp !

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();

Nov 17 '05 #1
5 1105
<re**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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) ?
Nov 17 '05 #2
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!

--
http://sgomez.blogspot.com

Nov 17 '05 #3
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.

"Danny Tuppeny" <gr****@dannytuppeny.commmmmm> wrote in message
news:43**********************@ptn-nntp-reader02.plus.net...
<re**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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) ?

Nov 17 '05 #4
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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! :)
Nov 17 '05 #5
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

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TSM | last post by:
Hello, I'm writing a JSP application with an underlying Oracle Database. I have the following problem: the same SQL statement works fine in the SQL+ console and in SQL Navigator, but it fails in...
6
by: oraustin | last post by:
My code, visual basic 6.3 for access 2002 SP3, returns a large recordset strSQL = "SELECT * FROM tblMobileCalls WHERE Client = '" & strClient & "'" rstCalls.Open strSQL,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.