Read XML and add element if missing

catlook

New member
Joined
Jun 28, 2007
Messages
4
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:

Code:
            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 wasnt 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!
 
simple answer is this:
  • create a new fragment in the target document; i think its 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.

Heres a longer answer though; youre 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)

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 doesnt find the element (or if the elements inner text is nothing)

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

so i now have filled in the missing snippet with :

Code:
            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 doesnt contain the added node. So, I must need a write or something. I found XmlDocument Write and WriteTo functions but wasnt sure if either applied.

So, how do I save my painstakingly added element?
 
I meant to say I found WriteContentTo and Write functions.

I was looking at WriteContentTo, and maybe thats 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, Ill sit quiet and wait now. Thanks for your assstance!
 
if you put a breakpoint and inspect the xdoc in memory, youll see that its there.

however, thats only in memory so you need to save your changes using:
doc.save(path)
after you have appended the frag
 
Back
Top