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

adding an XML node an already existing XMLDocument

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
I need to put this into the system

Code:
<overrides>
   <override index="1">
      <datetime></datetime>
      <classification></classification>
      <explanation></explanation>
   </override>
</overrides>

This node isn't already a tag in the document so I don't think I can use something like this

Code:
myXmlDoc.LoadXml(myReader("XML"))
myXmlDoc.SelectSingleNode("//dataobject/overrides/override[@index=1]/classification").InnerText = ddlOverride.SelectedValue.ToString

Any ideas?
 
You need to figure out where in the doc you want it.
For your case it may be easier to open the xml file as a regular flat file, find the location you are interested in and just insert the xml.

-Sometimes the answer to your question is the hack that works
 
is there a quick way of doing that?
 
Have you done any actual coding with the XML file object? I can't figure out whether you know what you are doing but are looking for suggestions, or if you are asking how to modify a snippet of code you found somewhere.

-Sometimes the answer to your question is the hack that works
 
Yeah, I actually wrote quite a bit using an XMLWriter. That snippet of code was from my project, but I found out that the tag was not to spec for the SDK I am using.

I could the text XML from SQL and manipulate it with some String Method (any idea which one I could use, but is there a better way in the XMLDocument object? This is the first project where I have worked so extensively on XML, mainly because of the SDK.
 
You should be able to add the node like you say you don't think you can.

Otherwise you should be able to open the doc at the specific node, then just create a new node and add the new node to the existing node.

-Sometimes the answer to your question is the hack that works
 
Otherwise you should be able to open the doc at the specific node, then just create a new node and add the new node to the existing node."

So if I selected the "dataobject" node. What is the method to add the node that I create? Like I said, I did quite a bit with the XMLDocument object, but I am somewhat of a novice, as you can probably tell.
 
You should already have the xml namespace imported so it's very literal:

psuedocode:
Dim a as new XML.node
a.text ="<my text here>/<myText>"
parent.AddNode(a)
parent.save



-Sometimes the answer to your question is the hack that works
 
System.NullReferenceException: Object reference not set to an instance of an object. at _Default.btnReport_Click(Object sender, EventArgs e)"

This is what I get as an error when I try to run the code that I made at the beginning. I believe these errors are tied to the errors that I mentioned. I need to add the node a different way because I cannot select a node that doesn't exist in the XML.
 
That pseudocode doens't help. I am asking for the specific method to use.
 
dashen, do you know how to debug code?

You said that the node didn't exist, but you try to select it anyway. You are going to have to select the deepest node available and add new nodes as you go.
This is where you add code to check if a node exists, and if it does, seleect the next deeper node, otherwise create a new node.

People here aren't going to do your job, that's what you get paid for. I will nudge firmly in the right direction, but I'm not writing your code for you, I have enough of my own to write each day.

-Sometimes the answer to your question is the hack that works
 
So, you can't even suggest a method in the object library to use?
 
I am not even asking for you to code the project. You give me pseudo code, which does not help me in what I am trying to do. Considering I have a lot of code already, in the XML format, I already have pseudo code on how to do this, but I am running into an issue of what method is available for this.

I have tried InsertAfter after selecting the "dataobjects" node, but I don't know the method to set the new node to what I need.
 
Look in the stack trace for your null ref. you are attempting to access a node that doesn't exist. This means you need logic to verify that the node exists, and IF IT DOESN'T then you need to do something else, like create the nodes in question.

You can check, without throwing an exception whether or not a node exists.

-Sometimes the answer to your question is the hack that works
 
Which points me back to my original question as to what is the specific method that would allow me to create those aforementioned nodes.

I wrote the XML using XMLWriter, but if the document already exists, what method do I use to put a new node into the document?
 
Meh, I give up on trying to use the XML format. I am just going to string manipulate it and pass it as an IOStream and write is as an XML. Not eloquent like I wanted, but it will get the job done. Thanks for the help anyways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top