Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3981584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:25:39+00:00 2026-05-20T05:25:39+00:00

I got XML like this: <Type> <Connections> <Conn ServerName=serv1 DataBase=Persons User=admin Pass=123/> <Conn ServerName=serv2

  • 0

I got XML like this:

 <Type> 
   <Connections>
      <Conn ServerName="serv1" DataBase="Persons" User="admin" Pass="123"/>
      <Conn ServerName="serv2" DataBase="Type123" User="admin" Pass="123"/>
   </Connections>
   <UDLFiles />
 </Type> 

so I load the xml like this

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);

and I have given a combination of ServerName, DataBase, User and Pass.

How to check in the XML if I have already added this combination as a <Conn> (those are 4 attributes) and how to add it if I havent added it yet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:25:40+00:00Added an answer on May 20, 2026 at 5:25 am

    Another option (if you are using .Net 3.5 or higher) is to skip XmlDocument and use LINQ to XML which, in my opinion, results in cleaner code:

    // Load the XML from file
    XElement docElem = XElement.Load(path);
    // Get the Connections element. This code assumes there will always be exactly one.
    XElement connectionsElem = docElem.Elements("Connections").Single();
    
    // Check if there is already a Conn element with the required attribute value combination
    if (!connectionsElem.Elements("Conn").Any(connElem =>
        (string)connElem.Attribute("ServerName") == serverName &&
        (string)connElem.Attribute("DataBase") == dataBase &&
        (string)connElem.Attribute("User") == user &&
        (string)connElem.Attribute("Pass") == pass)) {
    
        // Otherwise add such a Conn element
        connectionsElem.AddFirst(
            new XElement("Conn",
                new XAttribute("ServerName", serverName),
                new XAttribute("DataBase", dataBase),
                new XAttribute("User", user),
                new XAttribute("Pass", pass)
            )
        );
    }
    
    // Write the XML to file again.
    docElem.Save(path);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got XML that looks like this: <item> <itemDate>07/10/2009</itemDate> </item> I would like to
I have got an XML document which looks something like this. <Root> <Info>....</Info> <Info>....</Info>
i got a webservicedefinition like this: [OperationContract] [FaultContract(typeof(Exception))] [WebInvoke(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
I got a string from parsing a XML file which looks like this: Fri,
I got this in a XML field in sql server 2005 <Details> <Attribute Type=org>6800</Attribute>
I got an array that outputs data into an xml. It looks like this:
I do marshaling and unmarshaling messages. I have XML type like this: @XMLAccesorType(AccesorType.FIELD) @XMLType(name=Header)
Okay, so I've got some example xml that looks like this: <Node name=details> <Node
I got a XML document with a lot of data divided into category->subcategory. Like
I've got some XML I'm trying to import with c#, which looks something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.