Removing CDATA tag from XmlNode - c#

I have an XmlNode which represents the following xml for example:
XmlNode xml.innerText =
<book>
<name><![CDATA[Harry Potter]]</name>
<author><![CDATA[J.K. Rolling]]</author>
</book>
I want to change this node so that it'll contain the following:
XmlNode xml.innerText =
<book>
<name>Harry Potter</name>
<author>J.K. Rolling</author>
</book>
Any ideas?Thanks!

well, if it's exactly how you put it, then it's easy:
xml.innerText = xml.innerText.Replace("![CDATA[","").Replace("]]","");
xmlDoc.Save();// xmlDoc is your xml document

I suggest you to read your entire xml and rewrite it. You can read values without cdata like this
foreach (var child in doc.Root.Elements())
{
string name = child.Name;
string value = child.Value
}

Related

C# XmlDocument how to get pointer to root element and iterate over children

In XML Document:
Foo.xml
<products>
<product>
<id>1</id>
<name>Foo</name>
</product>
<product>
<id>2</id>
<name>Bar</name>
</product>
</products>
How to get this root element, iterate over his child elements and get their properties?
Bar.cs
XmlDocument doc = new XmlDocument();
doc.Load(path + "/foo.xml");
XmlNode mainNode = doc.DocumentElement.SelectSingleNode("products");
XmlNode root = mainNode.FirstChild; //null
foreach (XmlNode node in mainNode)
{
int id = Convert.ToInt32(node["id"].InnerText);
string name = node["name"].InnerText);
list.Items.Add(id);
list.Items.Add(name);
}
This code implicates that mainNode is null. What is the best practise of doing that?
The DocumentElement is the outermost element of the XML, i.e. the <products> element. You can't select another <products> element below it.
What you can do:
XmlNode mainNode = doc.SelectSingleNode("products");
or
XmlNode mainNode = doc.DocumentElement;
or
XmlNode mainNode = doc.DocumentElement.SelectSingleNode("//products");
The second one is probably the fastest, since it does not need to parse and process a query. The last one is overkill and should be avoided for clean code reasons (KISS principle).

Get childNode in childNode in XMLDocument

I have XML response from service and I need to get value of tag that exist in child node that this child node is a child node.
For example: This is an example of xml.
<ashrait>
<response>
<command>inquire</command>
<inquire>
<row>
<ResponseCode>000</ResponseCode>
<ResponseText>
Permitted.
</ResponseText>
<ResponseXML>
<ashrait>
<response>
<message>Permitted .</message>
<userMessage>Permitted .</userMessage>
</response>
</ashrait>
</ResponseXML>
</row>
</inquire>
</response>
</ashrait>
I need the the value in tag "userMessage" that exists in tag "ResponseXML".
I know that to get the node of "ResponseXML" I need for those lines:
var doc = new XmlDocument();
doc.LoadXml(responseFile);
ChildNode result = doc.GetElementsByTagName("ResponseXML")[0];
But how i get the tag userMessage in childNode "ResponseXML"?
Thanks
UPDATE:
I figured out how to do it.
Search for all the children with the tag and choose the order they want.
Create a model for your XML, load that model using XmlSerializer.
Check this Microsoft Docs.
https://learn.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization
Use
SelectNodes method: https://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx
or SelectSingleNode method: https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectsinglenode(v=vs.110).aspx
var doc = new XmlDocument();
doc.LoadXml(Xml);
XmlNode xn = doc.SelectSingleNode("//ashrait//inquire//row//ResponseXML//message");
var innerText = xn.InnerText;

Get specific values from Xml

I don't how to extract the values from XML document, and am looking for some help as I'm new to C#
I am using XmlDocument and then XmlNodeList for fetching the particular XML document
Here is my code
XmlNodeList XMLList = doc.SelectNodes("/response/result/doc");
And my XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result>
<doc>
<long name="LoadID">494</long>
<long name="EventID">5557</long>
<str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
<doc>
<long name="LoadID">774</long>
<long name="EventID">5558</long>
<str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
</result>
</response>
In this i have to fetch every the XMLData data that is under every doc tag and i have to fetch last doc tag EventID.
var xml = XDocument.Parse(xmlString);
var docs = xml.Root.Elements("doc");
var lastDocEventID = docs.Last()
.Elements("long")
.First(l => (string)l.Attribute("name") == "EventID")
.Value;
Console.WriteLine ("Last doc EventId: " +lastDocEventID);
foreach (var doc in docs)
{
Console.WriteLine (doc.Element("str").Element("TransactionDate").Value);
}
prints:
Last doc EventId: 5558
2014-05-28T14:17:31.2186777-06:00
2014-05-28T14:17:31.2186777-06:00
You can use two XPath expressions to select the nodes you want. To answer each part of your question in turn:
To select all of the XMLData nodes:
XmlNodeList XMLList
= doc.SelectNodes("/response/result/doc/str[#name='XMLData']");
To select the last EventId:
XmlNode lastEventIdNode =
doc.SelectSingleNode("/response/result/doc[position() =
last()]/long[#name='EventID']");
If not all doc nodes are guaranteed to have an event id child node, then you can simply:
XmlNodeList eventIdNodes =
doc.SelectNodes("/response/result/doc[long[#name='EventID']]");
XmlNode lastNode = eventIdNodes[eventIdNodes.Count - 1];
That should give you what you've asked for.
Update;
If you want the XML data inside each strXml element, you can use the InnerXml property:
XmlNodeList xmlList
= doc.SelectNodes("/response/result/doc/str[#name='XMLData']");
foreach(XmlNode xmlStrNode in xmlList)
{
string xmlInner = xmlStrNode.InnerXml;
}
There's one result tag short in your xml.
Try using this. It's cleaner too imho
XmlNodeList docs = doc.SelectSingleNode("response").SelectSingleNode("result").SelectNodes("doc");
Then you can use a combination of SelectSingleNode, InnerText, Value to get the data from each XmlNode in your list.
For example if you want the EventID from the first doc tag:
int eventID = int.Parse(docs[0].SelectSingleNode("long[#name='EventID']").InnerText);

Read first root node from XML

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.

XML Parsing in C#

I want add the new node as parent node of the old nodes in XML using C#. for example node have the following XMl file:
<bookstore>
<books>
<author>
</author>
</books>
</bookstore>
like that now I want add the new like below:
<bookstore>
<newnode>
<books>
<author>
</author>
</books>
</newnode>
</bookstore>
Try this:-
XmlDocument doc = new XmlDocument();
doc.Load("BookStore.xml");
XmlElement newNode = doc.CreateElement("newnode");
doc.DocumentElement.AppendChild(newNode);
newNode.AppendChild(doc.SelectSingleNode("/bookstore/books"));
doc.Save("BookStore.xml");
Don't have VS here so can't confirm that this works but something like this:
XmlDocument xd = new XmlDocument();
xd.Load("oldxmlfile.xml");
XmlNode oldNode = xd["nameOfRootNode"];
xd.RemoveAll();
XmlNode newParent = xd.CreateNode("nodename");
newParent.AppendChild(oldNode);
xd.AppendChild(newParent);
xd.Save("newXmlFile.xml");
You can clone the old node, append the clone, and remove the original:
(edit; I forgot that AppendChild will move the node if it is already there... no need to clone and remove...)
XmlDocument doc = new XmlDocument();
// load the current xml
doc.LoadXml(xml);
// create a new "newnode" node and add it into the tree
XmlElement newnode = (XmlElement) doc.DocumentElement.AppendChild(doc.CreateElement("newnode"));
// locate the original "books" node and move it
newnode.AppendChild(doc.SelectSingleNode("/bookstore/books"));
// show the result
Console.WriteLine(doc.OuterXml);

Categories

Resources