xml, c#, add record

I have xml below.

How to add a new plan if state name = 'NJ' and type name = 'Term Life'? using c#?
plan name="test"
plan id = "123"
plan healthtier="test"


<Products>
	<State Name="NJ">
		<Type Name="Subsidized">
			<Carrier Name="Chip" ID="100000008">
				<Plan Name="Chip" ID="100000322" HealthTier="" />
			</Carrier>
		</Type>
		<Type Name="Term Life">
			<Carrier Name="Mutual of Omaha" ID="100000017">
				<Plan Name="Term Life Express" ID="100000259" HealthTier="" />
		 	</Carrier>
		</Type>
		
	</State>
<State Name="PA"> 
		<Type Name="In Market">
			<Carrier Name="2017 Capital Blue Cross" ID="100000134">
				<Plan Name="Silver PPO 4500/0/10" ID="100002633" HealthTier="Silver" />
		 	</Carrier>
			<Carrier Name="2017 Geisinger Health Plan" ID="100000135">
		 	</Carrier>
		 			
		</Type>
		<Type Name="Out Market">
			<Carrier Name="2017 Capital Blue Cross" ID="100000134">
				<Plan Name="Gold PPO 1000/0/20" ID="100002634" HealthTier="Gold" />
			</Carrier>
			<Carrier Name="2018 Capital Blue Cross" ID="948120063">
				<Plan Name="Gold PPO 1000/0/20" ID="100002634" HealthTier="Gold" />
			</Carrier>  
		</Type> 
		<Type Name="Term Life">
			<Carrier Name="Assurity" ID="100000005">
				<Plan Name="Non-Med Term 350" ID="100000230" HealthTier="" />
			</Carrier> 
		</Type> 
	</State>
</Products>

Open in new window

LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi ITsolutionWizard;

The following code snippet should do what you need.
// Used to find node to insert new plan
string stateName = "NB"; 
string typeName = "Term Life";
// Used to create new plan
string planName = "test";
string planId = "123";
string planHealthtier = "test";

// Load the XML from the file system
XDocument xdoc = XDocument.Load(@"C:\Working Directory\Products.xml");

// Query to find node
var result = (from node in xdoc.Root.Elements("State")
              where node.Attribute("Name").Value == stateName && 
                    node.Elements("Type").Any(n => n.Attribute("Name").Value == typeName)
              select node.Elements("Type")
                         .Where(n => n.Attribute("Name").Value == typeName)
                         .Elements("Carrier").FirstOrDefault()).FirstOrDefault();

// Test to see if node was found to insert new plan 
if (result != null)
{
    // Create the node to be inserted
    XElement newPlan = new XElement("Plan",  
        new XAttribute("Name", planName), 
        new XAttribute("ID", planId), 
        new XAttribute("HealthTier", planHealthtier)
        );
    
    // Insert the new plan into the XML document
    result.Add(newPlan);
    // Save the update XML document
    xdoc.Save("File path and name goes here");
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
sorry i forget to add one item (Carrier)

How to add a new plan if state name = 'NJ' and type name = 'Term Life'? using c#?
plan name="test"
plan id = "123"
plan healthtier="test"
plan = "Chip"

Can you help me? Thanks
ITsolutionWizardAuthor Commented:
and with your post codes, it always return empty from var result.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.