473,657 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert new XML data using an XPath

6 New Member
I'm new to C#/.NET. I am writing some C#/.NET (2.0) classes that will allow someone to get and set values in an XML file using an XPath to point to the location of that data. It's more complicated than this but for simplicity's sake, I've trimmed it down to this.

The problem I'm running into is I want to find the best way to add the data into the XML file if the XPath returns nothing. For example, some XPaths might already exist in the XML file, so I can easily use XmlDocument.Sel ectNodes() to give me a list of the nodes that match and then I can get/set the Value on that node if it's an attribute or InnerText if it's an element, for example.

So, if someone wants to set some data into the XML and SelectNodes() returns nothing, what's the best way for me to add that node where the XPath says it should go? There may be a simple way in the .NET 2.0 framework, but I haven't been able to find it. Since SelectNodes() returns nothing, I don't have a position to insert the data into. I thought of taking off the rightmost part of the XPath and try again, repeating until I get something back and then insert each piece back out to the full XPath, but that seemed kludgy. It would be cool to just have a CreateNode(XPat h, value) or something like that. Ideas?

Here's a simple example:

Expand|Select|Wrap|Line Numbers
  1. <a>
  2.   <b>
  3.     <c>Blah</c>
  4.   </b>
  5. </a>
XPath "/a/b/c" would allow us to get/set the Blah value. Fine.

XPath "/a/b/d" would return nothing but the user would want it to create <d> as a sibling of <c> and set/get its value.
Jan 18 '08 #1
5 6028
rcronk
6 New Member
To summarize, (and to get this thread back up to the top again so it has a chance of being answered ;-)) I want to be able to query with xpath and if nothing is found, I want to know, given that specific xpath, the easiest way to insert a node at that xpath location.
Jan 21 '08 #2
rcronk
6 New Member
Is anyone out there.......
Jan 24 '08 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Hi,
Have you tried the XPathDocument along with the XPathNavigator class?

More info can be found at How To Query XML with an XPath Expression by Using Visual C# .NET

I havent yet used this for editing xml files, but i hope it can be done.
Do post back if you face any further issues
Jan 24 '08 #4
rcronk
6 New Member
Hi,
Have you tried the XPathDocument along with the XPathNavigator class?

More info can be found at How To Query XML with an XPath Expression by Using Visual C# .NET

I havent yet used this for editing xml files, but i hope it can be done.
Do post back if you face any further issues
Unfortunately, I didn't see a way at that link to insert a node that doesn't currently exist by just using an xpath expression. Could you post a few sample source code lines in C# that would insert the node that the xpath "/a/b/d" would point to into the following XML (same example as above) just using the xpath :

Expand|Select|Wrap|Line Numbers
  1. <a>
  2.   <b>
  3.     <c>Blah</c>
  4.   </b>
  5. </a>
The output XML after the insert would be:

Expand|Select|Wrap|Line Numbers
  1. <a>
  2.   <b>
  3.     <c>Blah</c>
  4.     <d></d>
  5.   </b>
  6. </a>
I have yet to find a way to insert <d></d> without parsing the xpath myself to get it down to "/a/b" to find a place where I could insert the "d" child - which I also have to parse off of the xpath. I don't want to parse xpath expressions manually in order to be able to insert a non-existent node because either I'll have to write a full xpath parser to be sure I'm doing it correctly, or it will fail when it comes across a more complicated xpath as input.
Jan 25 '08 #5
rcronk
6 New Member
bump - anyone out there?
Jan 28 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
52095
by: maltchev | last post by:
i need to insert data from an xml file into sql server table. the xml file contains only one record. how to insert the data? how to map the names of the fields in the xml file and the table? thank you in advance.
0
1127
by: Bob Rundle | last post by:
I'm using the XML Schema Designer to create a dataset. I can't get the relationships to work from the XML. To get them to work, I have to explicitly code them in. What's wrong here? Here is my test program...It will work only if you uncomment the Relations.Add line. Regards, Bob Rundle
0
3417
by: nima | last post by:
Hi I have two tables in my database. PRODUCT and PRODUCT_GROUP. PRODUCT has a foreign key to PRODUCT_GROUP. I generated a typed dataset using VS.net and added a relationship between the two tables. When I call my get WebMethod for the PRODUCT table I get the System.Data.ConstraintException exception. The get WebMethod for PRODUCT_GROUP works fine. When I remove the relationship the get WebMethod for PRODUCT works fine too. Both tables...
4
7595
by: Congero | last post by:
I'm trying to find a way to bind hierarchical data to a gridview control. I've been able to do this with some third party controls and was wondering if this functionality is available with the gridview control. Does anyone have a guidance on this? Thanks -- Live long, stay strong
11
6450
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
5
2870
nmm32
by: nmm32 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. I have another procedure which adds another row to the table, which is working fine inside the programming, and in the database. However, I still cannot figure out what am I doing wrong with the DataGrid configurations OR Database OR DataAdapter OR SqlSelectCommand ... I really don't know yet and I...
0
1766
by: cms3023 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. The table inside the database has data that matches with the parameter. I have another procedure which adds another row to the table, which is working fine inside the programming, and in the database. However, I still cannot figure out what am I doing wrong with the DataGrid configurations OR...
1
1522
by: Anilsg | last post by:
Hi all, I have the DropDownList in the FormView,inside InsertTemplateField The data is binded in run time to it in codebehind page,FOr the First time the page is loaded the DropdownList is not empty. when i click on the insert Link the data should be displayed in the gridView (Which is below the formview), But when i click on the Insert link the data is displayed in the gridView, But the DropdownList is empty. Here is the...
0
1212
by: Anilsg | last post by:
Hi all, I have the DropDownList in the FormView,inside InsertTemplateField The data is binded in run time to it in codebehind page,FOr the First time the page is loaded the DropdownList is not empty. when i click on the insert Link the data should be displayed in the gridView (Which is below the formview), But when i click on the Insert link the data is displayed in the gridView, But the DropdownList is empty.
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.