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 7889321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:04:07+00:00 2026-06-03T06:04:07+00:00

Currently i am using xpath with linq to xml to add elements after a

  • 0

Currently i am using xpath with linq to xml to add elements after a particular element in a xml document.
My concern is if this hierarchy is too deep , i have to mention the complete hierachy always?
Can’t i directly somehow give the name of the Element after which i want to add my new element.

In this code i am talking about this hierarchy:
SESSIONANALYSER/AP/MODES/NI/STATEINFORMATION/INIT/INPUT[@VALUE=’SLN7DEACTIVATION’]/METHOD

String strFile = File.ReadAllText(Server.MapPath("~/SessionAnalyserAccessories.xml"));
            XDocument xdoc1 = XDocument.Parse(strFile);
            xdoc1.XPathSelectElement("SESSIONANALYSER/AP/MODES/NI/STATEINFORMATION/INIT/INPUT[@VALUE='SLN7DEACTIVATION']/METHOD").AddAfterSelf(new XElement("METHOD", "", new XAttribute("NAME", "CHECKSUBSCRIBERBALANCE1"), new XAttribute("PARAMETERS", "SLN7"), new XAttribute("POSSIBLEACTIONS1", "FALLOUTON_FAILUREREGION_SETPSOFAILED_FALSE1"), new XAttribute("MAINTAINSESSION", "NO"), new XAttribute("RESPONSE", "-4"), new XAttribute("NEXTLEVEL", "")));
  • 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-06-03T06:04:08+00:00Added an answer on June 3, 2026 at 6:04 am

    Well with XPath you can use descendant::INPUT[@VALUE='SLN7DEACTIVATION']/METHOD or //INPUT[@VALUE='SLN7DEACTIVATION']/METHOD to find elements at any level thus if you are sure there is only one such element you can shorten your path that way. However if all the ancestors matter to identify the element then use the long path.

    On a side note, instead of

    String strFile = File.ReadAllText(Server.MapPath("~/SessionAnalyserAccessories.xml"));
    XDocument xdoc1 = XDocument.Parse(strFile);
    

    simply do

    XDocument xdoc1 = XDocument.Load(Server.MapPath("~/SessionAnalyserAccessories.xml"));
    

    that allows the XML parser to find the right encoding in the XML declaration of the document and should be more efficient by not first reading in a lengthy string with all XML.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm adding elements to my XmlDocument using XPath notation for which I've written
I'm currently using the following XAML code to add my xml file as a
I am navigating this office open xml file using XPath 1.0 (extract): <sheetData ref=A1:XFD108>
Currently using MySQL version 5.1.6 This is my first real world build and so
I have an XML file like below , using xpath and xnavigator how can
From http://metacpan.org/pod/XML::LibXML::Node : find evaluates the XPath 1.0 expression using the current node as
How reliable is this method? I'm currently using it and it pulls in my
I currently have this XML schema : <PSC5> <POI_ORI> <CIT>LIM</CIT> </POI_ORI> </PSC5> if user
I am trying to parse an xml feed using xpath. The feed contains categories
I'm currently working on a little harvester, using this dataset of 2700 foundations .

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.