Adding node to existing xml element - c#

I'm getting this error when trying to append a node to existing element in my xml document.The error is: Object reference not set to an instance of an object.
<houses>
<house windowsc="three">
<wind>0</wind>
<windows>
</windows>
</house>
</houses>
Code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("C:\\Houseplans.xml");
XmlElement xhousing = xDoc.DocumentElement["houses/house[#windowsc=\"three\"]/windows"];
XmlNode xName = xDoc.CreateElement("Name");
xName.InnerText = "hi";
xhousing.AppendChild(xName);

You want to use SelectSingleNode:
XmlNode xhousing = xDoc.SelectSingleNode(#"//house[#windowsc='three']/windows");
XmlNode xName = xDoc.CreateElement("Name");
xName.InnerText = "hi";
xhousing.AppendChild(xName);

Related

XmlDocument XPath expression failing

I am using C# XmlDocument API.
I have the following XML:
<Node1>
<Node2>
<Node3>
</Node3>
</Node2>
</Node1>
I want to get Node3 as an XmlNode. But my code is returning null:
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode root_node = doc.DocumentElement.SelectSingleNode("/Node1");
Log(root_node.OuterXml);
XmlNode test_node = root_node.SelectSingleNode("/Node2/Node3");
if (test_node == null)
Logger.Log.Error(" --- TEST NODE IS NULL --- ");
The log for root_node.OuterXml logs
<Node1><Node2><Node3>.....
But test_node returns null.
What is going wrong here?
Use the path "Node2/Node3" instead of "/Node2/Node3":
XmlNode test_node = root_node.SelectSingleNode("Node2/Node3");
In an XPath expression, a leading forward slash / represents the root of the document. The expression "/Node2/Node3" doesn't work because <Node2> isn't at the root of the document.
Use // instead of /, when you are selecting from the root node
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode root_node = doc.DocumentElement.SelectSingleNode("/Node1");
XmlNode test_node = root_node.SelectSingleNode("//Node2/Node3");
Another option is to use full path to the node 3
XmlNode test_node = doc.DocumentElement.SelectSingleNode("/Node1/Node2/Node3");
You can simple call Descendants()
var xml= #"<Node1><Node2><Node3></Node3></Node2></Node1>";
XDocument doc = XDocument.Parse(xml);
var node = doc.Descendants("Node3");
or use Element() starting from Root
var node2= doc.Root.Element("Node2").Element("Node3");
or use XPathSelectElement()
var node3= doc.XPathSelectElement("/Node1/Node2/Node3");

Formatting XML with XmlDocument

I am trying to create an XML file using XmlDocument that will then be passed to an API. Most of the data outputs correctly however there is one element that does not, no matter what I try.
Here is my current output
<agentInventoryReference xmlns="demo.org.uk/demo/AgentInventoryReference"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<agentInventoryIdentity xmlns="">
<shed xmlns="demo.org.uk/demom/AgentInventoryIdentifier">TAX</shed>
<arrivalPort xmlns="demo.org.uk/demo/AgentInventoryIdentifier">LHR</arrivalPort>
<masterAirwayBillPrefix xmlns="demo.org.uk/demo/AgentInventoryIdentifier">125</masterAirwayBillPrefix>
<masterAirwayBillNumber xmlns="demo.org.uk/demo/AgentInventoryIdentifier">12345678</masterAirwayBillNumber>
<nominatedAgent xmlns="demo.org.uk/demo/AgentInventoryIdentifier">DRB</nominatedAgent>
</agentInventoryIdentity>
I need to drop the xmlns="" at the end of the opening agentInventoryIdentity element.
My code looks like this:
XmlDocument generateInventoryXml = new XmlDocument();
XmlElement root = generateInventoryXml.CreateElement ("agentInventoryReference", "demo.org.uk/demo/AgentInventoryReference");
root.SetAttribute("xmlns", "demo.org.uk/demo/AgentInventoryReference");
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
generateInventoryXml.AppendChild(root);
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity");
XmlElement shed = generateInventoryXml.CreateElement("shed", "demo.org.uk/Sequoia/AgentInventoryIdentifier");
XmlElement arrivalport = generateInventoryXml.CreateElement("arrivalPort", "demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement masterAirwayBillPrefix = generateInventoryXml.CreateElement("masterAirwayBillPrefix","demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement masterAirwayBillNumber = generateInventoryXml.CreateElement("masterAirwayBillNumber", "demo.org.uk/demo/AgentInventoryIdentifier");
XmlElement nominatedAgent = generateInventoryXml.CreateElement("nominatedAgent", "demo.org.uk/demo/AgentInventoryIdentifier");
shed.InnerText = shedCode;
arrivalport.InnerText = arrivalPort;
masterAirwayBillPrefix.InnerText = awbPrefix;
masterAirwayBillNumber.InnerText = awbNumber;
nominatedAgent.InnerText = agent;
agentInventoryIdentity.AppendChild(shed);
agentInventoryIdentity.AppendChild(arrivalport);
agentInventoryIdentity.AppendChild(masterAirwayBillPrefix);
agentInventoryIdentity.AppendChild(masterAirwayBillNumber);
agentInventoryIdentity.AppendChild(nominatedAgent);
generateInventoryXml.DocumentElement.AppendChild(agentInventoryIdentity);
GenerateInventoryXml = generateInventoryXml.InnerXml;
Try this:
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity",generateInventoryXml.DocumentElement.NamespaceURI);`
Or
XmlElement agentInventoryIdentity = generateInventoryXml.CreateElement("agentInventoryIdentity","demo.org.uk/demo/AgentInventoryReference");

C# and XML: Why is XMLNS attirubute set to all child nodes?

Here is what I have attempted:
Creating elements:
XmlNode xHeader = xDoc.CreateElement("Customer");
XmlNode xCustomerID = xDoc.CreateElement("Customer_ID", strListName);
XmlNode xName = xDoc.CreateElement("Full_Name");
XmlNode xEmail = xDoc.CreateElement("Email");
XmlNode xHomeAddress = xDoc.CreateElement("Home_Address");
XmlNode xMobileNumber = xDoc.CreateElement("Mobile_Number");
Appending nodes to document.
xDoc.DocumentElement.AppendChild(xHeader);
xHeader.AppendChild(xCustomerID);
xCustomerID.AppendChild(xEmail);
xCustomerID.AppendChild(xHomeAddress);
xCustomerID.AppendChild(xMobileNumber);
This is what the is generated in the XML. http://pastebin.com/dNs8Ueiw
I want there to be no xmlns = "" in the child nodes of Customer_ID.
If you want XML of:
<Customer_ID xmlns="a">
<Email>
</Email>
<Home_Address>
</Home_Address>
<Mobile_Number>
</Mobile_Number>
</Customer_ID>
... then you need to make sure your Email, Home_Address and Mobile_Number elements are all in the same namespace as your Customer_ID element:
XmlNode xCustomerID = xDoc.CreateElement("Customer_ID", strListName);
XmlNode xEmail = xDoc.CreateElement("Email", strListName);
XmlNode xHomeAddress = xDoc.CreateElement("Home_Address", strListName);
XmlNode xMobileNumber = xDoc.CreateElement("Mobile_Number", strListName);
Basically you're seeing the result of namespace defaulting - unless an xmlns=... is specified for an element, it inherits the namespace of its parent.
(Also note that if you can, you should use LINQ to XML - it's a much more pleasant XML API, with nicer namespace handling.)

How to use XmlNamespaceManager and SelectSingleNode correctly?

I need to get node value from xml. The xml has namespace.
I have the following code
string xml =
"<file xmlns=\"SFAKT\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
"<document>test</document>" +
"</file>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDocument.NameTable);
ns.AddNamespace("sf", "SFAKT");
XmlNode node = xmlDocument.SelectSingleNode("sf:file/document");
But node = null
Can you tell me where is the error in my code?
You need to use the overloaded SelectSingleNode method and pass in the XmlNamespaceManager. Also, you need the sf prefix for the document node.
Pull the node out like this:
XmlNode node = xmlDocument.SelectSingleNode("sf:file/sf:document", ns);

Change XML root element name

I have XML stored in string variable:
<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>
Here I want to change XML tag <ItemMasterList> to <Masterlist>. How can I do this?
System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;
I know i am a bit late, but just have to add this answer as no one seems to know about this.
XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
doc.Root.Name = "MasterList";
Which returns the following:
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:
XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");
XDocument result = new XDocument(
new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
Using the XmlDocument way, you can do this as follows (and keep the tree intact):
XmlDocument oldDoc = new XmlDocument();
oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
XmlDocument newDoc = new XmlDocument();
XmlElement ele = newDoc.CreateElement("MasterList");
ele.InnerXml = node.InnerXml;
If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:
// Create a new Xml doc object with root node as "NewRootNode" and
// copy the inner content from old doc object using the LastChild.
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("NewRootNode");
docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
newRoot.InnerXml = oldDoc.LastChild.InnerXml;
string xmlText = docNew.OuterXml;

Categories

Resources