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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing an XML Configuration file

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Nov 21, 2003
4,772
US
I have an XML file for email sending that I need to parse.
Normally, this would be pretty straight forward, except this particular file has several entries, with an "IsEnabled" tag....
So, how would I go about finding the first node that is enabled?

Here's (partially) what the file looks like:
Code:
<SmtpClientsConfiguration xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xmlns:xsd="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
	<SmtpDefinitions>
		<SmtpClientDefinition>
			<SmtpHost>mail.xxxx.net</SmtpHost>
			<SmtpPort>25</SmtpPort>
			<SmtpUser>user@xxxx.net</SmtpUser>
			<SmtpPassword>password</SmtpPassword>
			<SmtpDomain />
			<SmtpSender>user@xxxx.net</SmtpSender>
			<UseSSL>false</UseSSL>
			<IsEnabled>true</IsEnabled>
			<Timeout>30</Timeout>
		</SmtpClientDefinition>
		<SmtpClientDefinition>
			<SmtpHost>mail.xxxx.org</SmtpHost>
			<SmtpPort>25</SmtpPort>
			<SmtpUser>user@xxxx.org</SmtpUser>
			<SmtpPassword>password</SmtpPassword>
			<SmtpDomain />
			<SmtpSender>user@xxxx.org</SmtpSender>
			<UseSSL>false</UseSSL>
			<IsEnabled>false</IsEnabled>
			<Timeout>30</Timeout>
		</SmtpClientDefinition>

... as you can see, the first node has "IsEnabled" set to True, however it may not always be the first node.
So, I guess what I need to do is find the first block with "IsEnabled" set to True, then find the parent of that block, and get the configuration settings from that block.

I know... it should be simple... but I haven't done a lot of XML parsing, especially in C#.

Thanks in advance!


Just my 2ó

"What the captain doesn't realize is that we've secretly replaced his Dilithium Crystals with new Folger's Crystals."

--Greg
 
Without Linq:
With Linq: (I know this one isn't specifically about reading the document, but it shows you a code snippet that I think is one of the simplest approaches - using a custom object, which you then select all of the XML nodes into an enumeration of).

Stack overflow also has a large number of posts on this. Part of what you need to decide is what version of the .NET framework you're going to limit yourself to. The XML Linq stuff is quite useful, but requires higher levels of .NET the more of the features you make use of.
 
I would use an XPath for that, e.g.
Code:
myXml.SelectNodes("//SmtpClientDefinition[IsEnabled[text()='true']]");

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top