Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

My xml layout has changed since going into production. So, i need to read the file and, if the Facility element is missing, add it in.

 

Old xml:

<SrvConfig>

<HostAddress>dev123.home.com</HostAddress>

<WhsNumber>03</WhsNumber>

<SrvPath>/appl/03/wt4090/</SrvPath>

</SrvConfig>

 

Desired xml:

<SrvConfig>

<HostAddress>dev123.home.com</HostAddress>

<WhsNumber>03</WhsNumber>

<SrvPath>/appl/03/wt4090/</SrvPath>

<Facility>MFG</Facility>

</SrvConfig>

 

So my code to read it:

 

            XmlNode node;
           XmlDocument doc = new XmlDocument();

           //open xml file
           doc.Load(srvCfgPathAndName);

           //find element of interest
           node = doc.SelectSingleNode("/SrvConfig/HostAddress");
           _currServerIP = node.InnerText;
           node = doc.SelectSingleNode("/SrvConfig/WhsNumber");
           _currWhs = node.InnerText;
           node = doc.SelectSingleNode("/SrvConfig/SrvPath");
           _currPath = node.InnerText;

           //since Facility wasn't part of original design we 
           //may need to add it for existing devices
           node = doc.SelectSingleNode("/SrvConfig/Facility");
           if (node == null)
           {
                 //somehow insert it and write it back out  
           }

 

Can someone helpme out with the insert and write bit? Thanks!

Posted

simple answer is this:

  • create a new fragment in the target document; i think it's XmlDocumentFragment in vb, but that should get you pointed in the right direction

  • set the INNER XML of your fragment ("<Facility>someval</Facility"> or "<Facility />")

  • then you need the parent node you want to add it to (in this case SrvConfig)


  • and tell the parent node to append the fragment.

 

Here's a longer answer though; you're bound to run into this problem over and over and over and well you get the idea.

 

So ideally you would create a class to handle this, but you could get by with just two functions. The basic logic is like this, you create a get and a set that do the following (lazy pseudo-code)

 

getInfo(parentNode: XmlNode, elementName: String): String
 targetNode=parentNode.selectSingleNode(elementName)

 if targetNode is nothing
   return String.Empty
 else
   return targetNode.innerText
 end if

end getInfo


setInfo(parentNode: XmlNode, elementName: String, value: String): Void
 targetNode=parentNode.selectSingleNode(elementName)
 
 if targetNode is nothing
   // use solution from my short answer above to create the targetnode, ending it with: 
   targetNode = parentNode.addChild(xFrag)
 end if

 // now you have a targetNode so...
 targetNode.innerText = value

end setInfo

 

 

now what you have is:

 

a get function getInfo that returns String.Empty when it doesn't find the element (or if the element's inner text is nothing)

 

a set function that will create the node if DNE and then set the value you want.

Posted

Great notes!!

 

so i now have filled in the missing snippet with :

 

            node = doc.SelectSingleNode("/SrvConfig/Facility");
           if (node == null)
           {
               XmlDocumentFragment myFrag = doc.CreateDocumentFragment();
               myFrag.InnerXml = "<Facility>MFG</Facility>";
               XmlNode parentNode;
               parentNode = doc.SelectSingleNode("/SrvConfig");
               parentNode.AppendChild(myFrag);
           }

 

It compiles and runs without error, however the xml file doesn't contain the added node. So, I must need a write or something. I found XmlDocument Write and WriteTo functions but wasn't sure if either applied.

 

So, how do I save my painstakingly added element?

Posted

I meant to say I found WriteContentTo and Write functions.

 

I was looking at WriteContentTo, and maybe that's the right direction. ONly, does this mean I have to reopen the document as a FileStream, use that to instantiate a XmlTextWriter and then rewrite each element? Or how does it know to just add the one at the end?

 

Ok, I'll sit quiet and wait now. Thanks for your assstance!

Posted

if you put a breakpoint and inspect the xdoc in memory, you'll see that it's there.

 

however, that's only in memory so you need to save your changes using:

doc.save(path)

after you have appended the frag

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...