remove root node but keep all child nodes - c#

I have this xml that is parsed through
<ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate">
<Test>
<CurrencyCode>SEKAUDPMI</CurrencyCode>
<CurrencyType>AUD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>6.5656</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKBRLPMI</CurrencyCode>
<CurrencyType>BRL</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>2.4376</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKCADPMI</CurrencyCode>
<CurrencyType>CAD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>7.0771</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKCHFPMI</CurrencyCode>
<CurrencyType>CHF</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>951.7346</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
</ns0:Root>
The xml has ("Test") child nodes that occure a lot of time.
What I need to do is actually remove (" <ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate"> ")
So that the XML output is
<Test>
<CurrencyCode>SEKAUDPMI</CurrencyCode>
<CurrencyType>AUD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>6.5656</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKBRLPMI</CurrencyCode>
<CurrencyType>BRL</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>2.4376</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
What I've tried to do is
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());
System.Xml.XmlNode xNode = xDoc.FirstChild;
System.Xml.XmlNode xParent = xNode.ParentNode;
System.Xml.XmlNodeList xChilds = xNode.ChildNodes;
xDoc.RemoveChild(xNode);
foreach (System.Xml.XmlNode node in xChilds)
{
xParent.AppendChild(node);
}
But the main problem is that the returned xml only contains one child ("Test") and not the others.
I have also tried using Linq where I found this on a post
XDocument input = XDocument.Load("input.xml");
XElement firstChild = input.Root.Elements().First();
But this returns the same thing and does not help my case.
Can someone help me in the right direction?
Thanks.

If you really only need the text string, the fastest way to achieve this is using an XmlReader on the Root XElement:
XDocument xDoc = XDocument.Parse(bizobj.Message.BodyPart.GetOriginalDataStream());
XElement root = xDoc.Root;
XmlReader reader = root.CreateReader();
reader.MoveToContent();
string txt = reader.ReadInnerXml();

Just putting my final code here if anyone wants to take a look and use it.
Filburt was the one that threw me in the right direction anyway so look as his aswell.
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());
string output = xDoc.DocumentElement.InnerXml;
byte[] byteArray = Encoding.ASCII.GetBytes(output);
MemoryStream stream = new MemoryStream(byteArray);
stream.Position = 0;
bizobj.Message.BodyPart.Data = stream;
return bizobj;

Related

how can append to xml

i have this xml.
<project>
<user>
<id>1</id>
<name>a</name>
</user>
<user>
<id>2</id>
<name>b</name>
</user>
</project>
now how can append a new element like this between element <project></project>
<user>
<id>3</id>
<name>c</name>
</user>
string xml =
#"<project>
<user>
<id>1</id>
<name>a</name>
</user>
<user>
<id>2</id>
<name>b</name>
</user>
</project>";
XElement x = XElement.Load(new StringReader(xml));
x.Add(new XElement("user", new XElement("id",3),new XElement("name","c") ));
string newXml = x.ToString();
If you mean using C# then probably the simplest way is to load the xml up into an XmlDocument object and then add a node representing the additional element.
e.g. something like:
string filePath = "original.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlElement root = xmlDoc.DocumentElement;
XmlNode nodeToAdd = doc.CreateElement(XmlNodeType.Element, "user", null);
XmlNode idNode = doc.CreateElement(XmlNodeType.Element, "id", null);
idNode.InnerText = "1";
XmlNode nameNode = doc.CreateElement(XmlNodeType.Element, "name", null);
nameNode.InnerText = "a";
nodeToAdd.AppendChild(idNode);
nodeToAdd.AppendChild(nameNode);
root.AppendChild(nodeToAdd);
xmlDoc.Save(filePath); // Overwrite or replace with new file name
But you haven't said where the xml fragments are - in files/strings?
If you have the below XML file:
<CATALOG>
<CD>
<TITLE> ... </TITLE>
<ARTIST> ... </ARTIST>
<YEAR> ... </YEAR>
</CD>
</CATALOG>
and you have to add another <CD> node with all its child nodes:
using System.Xml; //use the xml library in C#
XmlDocument document = new XmlDocument(); //creating XML document
document.Load(#"pathOfXmlFile"); //load the xml file contents into the newly created document
XmlNode root = document.DocumentElement; //points to the root element (catalog)
XmlElement cd = document.CreateElement("CD"); // create a new node (CD)
XmlElement title = document.CreateElement("TITLE");
title.InnerXML = " ... "; //fill-in the title value
cd.AppendChild(title); // append title to cd
XmlElement artist = document.CreateElement("ARTIST");
artist.InnerXML = " ... ";
cd.AppendChild(artist);
XmlElement year = document.CreateElement("YEAR");
year.InnerXML = " ... ";
cd.AppendChild(year);
root.AppendChild(cd); // append cd to the root (catalog)
document.save(#"savePath");//save the document

XPathSelectElements Method yields no results?

string xml = #"<AllBands >
<Band>
<Name ID=""1234"" started=""1962"">Beatles<![CDATA[lalala]]></Name>
<Last>1</Last>
<Salary>2</Salary>
</Band>
<Band>
<Name ID=""222"" started=""1968"">Doors<![CDATA[lalala]]></Name>
<Last>1</Last>
<Salary>2</Salary>
</Band>
</AllBands>";
XmlReader reader = XmlReader.Create(new StringReader(xml));
XElement root = XElement.Load(reader);
var child1 = root.XPathSelectElements(#"/AllBands/Band" );
child1 has no childs !
What am i missing ?
root does not refer to the XML document, it refers to the AllBands element since you loaded it using XElement.Load(). So your XPATH should be relative to that element.
var children = root.XPathSelectElements("Band");

XML string to XML document

I have a whole XML document in a String which i need to convert to a XML document and parse tags in the document
This code sample is taken from csharp-examples.net, written by Jan Slama:
To find nodes in an XML file you can use XPath expressions. Method XmlNode.Selec­tNodes returns a list of nodes selected by the XPath string. Method XmlNode.Selec­tSingleNode finds the first node that matches the XPath string.
XML:
<Names>
<Name>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Name>
<Name>
<FirstName>James</FirstName>
<LastName>White</LastName>
</Name>
</Names>
CODE:
XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); // suppose that myXmlString contains "<Names>...</Names>"
XmlNodeList xnList = xml.SelectNodes("/Names/Name");
foreach (XmlNode xn in xnList)
{
string firstName = xn["FirstName"].InnerText;
string lastName = xn["LastName"].InnerText;
Console.WriteLine("Name: {0} {1}", firstName, lastName);
}
Using Linq to xml
Add a reference to System.Xml.Linq
and use
XDocument.Parse(string xmlString)
Edit: Sample follows, xml data (TestConfig.xml)..
<?xml version="1.0"?>
<Tests>
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
<Test TestId="0006" TestType="GUI">
<Name>Another Test</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>Test Input</Input>
<Output>10</Output>
</Test>
</Tests>
C# usage...
XElement root = XElement.Load("TestConfig.xml");
IEnumerable<XElement> tests =
from el in root.Elements("Test")
where (string)el.Element("CommandLine") == "Examp2.EXE"
select el;
foreach (XElement el in tests)
Console.WriteLine((string)el.Attribute("TestId"));
This code produces the following output:
0002
0006
Depending on what document type you want you can use XmlDocument.LoadXml or XDocument.Load.
Try this code:
var myXmlDocument = new XmlDocument();
myXmlDocument.LoadXml(theString);

Little help with XLinq

I have this XML:
<Test>
<element>toto</element>
<element>tata</element>
</Test>
How I can get the nodes "element"?. I see on the web I can get them with:
var elements = from element in xmlDoc.Descendants("element")
select element;
But "elements" is empty!
EDIT 1: I'm loading the XDocument with this exact XML:
<Test xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
<element>toto</element>
<element>tata</element>
</Test>
Ok well there's your problem, your names have to be qualified with the appropriate XML namespace.
XNamespace ns = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var elements = xmlDoc.Descendants(ns + "element");
It is probably a problem with how you are creating your XDocument object. The following code works fine for me:
var doc = XDocument.Parse(#"
<Test>
<element>toto</element>
<element>tata</element>
</Test>");
var elements = doc.Descendants("element");
//prints "2"
Console.WriteLine(elements.Count());

Why doesn't this XPath query returns any nodes?

I'm querying Sharepoint server-side and getting back results as Xml. I want to slim down the Xml into something more lightweight before sending it to jQuery through a WebMethod.
However my XPath query isn't working. I thought the following code would return all Document nodes, but it returns nothing. I've used XPath a little before, I thought //Document do the trick.
C# XPath query
XmlDocument xmlResults = new XmlDocument();
xmlResults.LoadXml(xml); // XML is a string containing the XML source shown below
XmlNodeList results = xmlResults.SelectNodes("//Document");
XML being queried
<ResponsePacket xmlns="urn:Microsoft.Search.Response">
<Response domain="QDomain">
<Range>
<StartAt>1</StartAt>
<Count>2</Count>
<TotalAvailable>2</TotalAvailable>
<Results>
<Document relevance="126" xmlns="urn:Microsoft.Search.Response.Document">
<Title>Example 1.doc</Title>
<Action>
<LinkUrl size="32256" fileExt="doc">http://hqiis99/Mercury/Mercury documents/Example 1.doc</LinkUrl>
</Action>
<Description />
<Date>2010-08-19T14:44:56+01:00</Date>
</Document>
<Document relevance="31" xmlns="urn:Microsoft.Search.Response.Document">
<Title>Mercury documents</Title>
<Action>
<LinkUrl size="0" fileExt="aspx">http://hqiis99/mercury/Mercury documents/Forms/AllItems.aspx</LinkUrl>
</Action>
<Description />
<Date>2010-08-19T14:49:39+01:00</Date>
</Document>
</Results>
</Range>
<Status>SUCCESS</Status>
</Response>
</ResponsePacket>
You're trying to select Document elements which don't have a namespace... whereas the default namespace is actually "urn:Microsoft.Search.Response" here.
I think you want something like this:
XmlDocument xmlResults = new XmlDocument();
xmlResults.LoadXml(xml);
XmlNamespaceManager manager = new XmlNamespaceManager(xmlResults.NameTable);
manager.AddNamespace("ns", "urn:Microsoft.Search.Response.Document");
XmlNodeList results = xmlResults.SelectNodes("//ns:Document", manager);
This finds two elements.
If you can use LINQ to XML instead, it makes it all somewhat easier:
XDocument results = XDocument.Parse(xml);
XNamespace ns = "urn:Microsoft.Search.Response.Document";
var documents = results.Descendants(ns + "Document");
I love LINQ to XML's namespace handling :)
Alternatively, you could try the following and ignore the namespaces:
XmlDocument xmlResults = new XmlDocument();
xmlResults.LoadXml(xmlString);
XmlNodeList results = xmlResults.SelectNodes("//*[local-name()='Document']");

Categories