How replace XML Node from one XML document with another XML node from another XML Document.
Please help..
You can use LINQ to Xml XElement.ReplaceWith method
// select node from one doc
XDocument xdoc1 = XDocument.Load(path_to_doc1);
XElement one = xdoc1.Descendants("One").First();
// select node from another doc
XDocument xdoc2 = XDocument.Load(path_to_doc2);
XElement another = xdoc2.Descendants("Another").First();
// replace one xml node with another
one.ReplaceWith(another);
xdoc1.Save(path_to_doc1);
Related
I have the following XML document structure.
<FareSearchResponse>
<AirlineList>
<AQR>...</AQR>
<ABA>...</ABA>
<AAI>...</AAI>
<A9W>...</A9W>
<AVS>...</AVS>
<AAF>...</AAF>
<AEY>...</AEY>
<ALH>...</ALH>
<AQF>...</AQF>
</AirlineList>
</FareSearchResponse>
I want to rename all child elements of AirlineList to Airline. The child name tags vary and can be any name. So there is no way of knowing which name tags will appear and what they will be. It is a dynamic XML document from a web service. So far I have managed to load the XML string into an XmlDocument;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(System.Web.HttpUtility.HtmlDecode(resultString));
XElement firstChild = xmlDoc.ToXDocument().Root.Elements().First();
I want to select all AirlineList elements and rename them to Airline.
I have added a more details. The XML document is now contained in firstChild. How do I replace elements in the firstChild and not directly from XmlDocument
Thanks.
I want to select all AirlineList elements and rename them to Airline.
Use Linq to Xml and update the Name for child elements.
var doc = XDocument.Load(filepath);
foreach (var element in doc.Descendants("AirlineList").Elements())
{
element.Name ="Airline";
}
Check this example
I work with three kinds of XML files :
Type A:
<?xml version="1.0" encoding="UTF-8"?>
<nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
</nfeProc>
Tyepe B:
<?xml version="1.0" encoding="UTF-8"?>
<cancCTe xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04">
</cancCTe>
Type C:]
<?xml version="1.0" encoding="UTF-8"?>
<cteProc xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04">
</cteProc>
I have try with this code to read the first node :
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(#"C:\crruopto\135120068964590_v01.04-procCTe.xml");
XmlNodeList ml = xmlDoc.GetElementsByTagName("*");
XmlElement root = xmlDoc.DocumentElement;
exti = root.ToString();
but dont return anything i want to read the first node , need to know if the file is nfeProc ,canCTE or cteProc
The second question is how i get the value from "value" in the same tag???
Thanks
From this post:
//Root node is the DocumentElement property of XmlDocument
XmlElement root = xmlDoc.DocumentElement
//If you only have the node, you can get the root node by
XmlElement root = xmlNode.OwnerDocument.DocumentElement
I would suggest using XPath. Here's an example where I read in the XML content from a locally stored string and select whatever the first node under the root is:
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(xml));
XmlNode node = doc.SelectSingleNode("(/*)");
If you aren't required to use the XmlDocument stuff, then Linq is your friend.
XDocument doc = XDocument.Load(#"C:\crruopto\135120068964590_v01.04-procCTe.xml");
XElement first = doc.GetDescendants().FirstOrDefault();
if(first != null)
{
//first.Name will be either nfeProc, canCTE or cteProc.
}
Working with Linq to XML is the newest and most powerful way of working with XML in .NET and offers you a lot more power and flexibility than things like XmlDocument and XmlNode.
Getting the root node is very simple:
XDocument doc = XDocument.Load(#"C:\crruopto\135120068964590_v01.04-procCTe.xml");
Console.WriteLine(doc.Root.Name.ToString());
Once you have constructed an XDocument you don't need to use any LINQ querying or special checking. You simply pull the Root property from the XDocument.
Thanks i have solved this way the first part
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(nomear);
XmlNodeList ml = xmlDoc.GetElementsByTagName("*");
XmlNode primer = xmlDoc.DocumentElement;
exti = primer.Name;
First, to be clear, you're asking about the root element, not the root node.
You can use an XmlReader to avoid having to load large documents completely into memory. See my answer to a how to find the root element at https://stackoverflow.com/a/60642354/1307074.
Second, once the reader is referencing the element, you can use the reader's Name property to get the qualified tag name of the element. You can get the value as a string using the Value property.
Using old way of doing via XmlDocument,
string xmlstring = "<root><profile><Name>John</Name><Age>23</Age></profile></root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlstring);
string childNodes = xmlDoc.SelectSingleNode("root/Profile").InnerXml;
// output of childNodes would be => "<Name>John</Name><Age>23</Age>";
what is the equivalent of doing the above execution in LinQ when you have XElement variable. I see XPathSelectElement method in XElement but it doesn't return the child nodes + Child nodes text. Any Ideas?
I wouldn't use XPath at all for this. I'd use:
XDocument doc = XDocument.Parse(xmlString);
var nodes = doc.Root
.Elements("profile")
.DescendantsAndSelf();
That give the profile nodes and all their descendants. It's not really clear what you're trying to do with the results, but if you can give more details I should be able to come up with the appropriate code.
I have two XmlDocuments. Something like:
<document1>
<inner />
</document1>
and
<document2>
<stuff/>
</document2>
I want to put document2 inside of the inner node of document1 so that I end up with a single docement containing:
<document1>
<inner>
<document2>
<stuff/>
</document2>
</inner>
</document1>
Here's the code...
XmlDocument document1, document2;
// Load the documents...
XmlElement xmlInner = (XmlElement)document1.SelectSingleNode("/document1/inner");
xmlInner.AppendChild(document1.ImportNode(document2.DocumentElement, true));
You can, but effectively a copy will be created. You have to use XmlNode node = document1.ImportNode(document2.RootElement), find the node and add node as a child element.
Example on msdn: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.importnode.aspx
No. You can only have on XmlDocument in an XML DOM. What you want to do is get the DocumentElement associated with document2 and append that XmlElement as a child to the XmlElement.
Using C#
How do you remove a specific node from an XMLDocument using XPATH?
If you want to delete nodes, that are not direct children of the documents root, you can do this:
XmlDocument doc = new XmlDocument();
// ... fill or load the XML Document
XmlNode childNode = doc.SelectSingleNode("/rootnode/childnode/etc"); // apply your xpath here
childNode.ParentNode.RemoveChild(childNode);
Here you go. ChildNodeName, could be just the node name or an XPath query.
XmlDocument doc = new XmlDocument();
// Load you XML Document
XmlNode childNode = doc.SelectSingleNode(childNodeName);
// Remove from the document
doc.RemoveChild(childNode);
There is a different way using Linq, but I guessed you were using .NET 2.0
XPath can only select nodes from a document, not modify the document.