C# - Convert an XML to a new XML with different structure - c#

I have an XML file that have nodes and child nodes, some type of a tree view. I would like to read these elements and extract the content to write them to a new XML with a new schema with lighter tree hirarchy.
In my code, I parse the XML file and read the nodes and child nodes but i can only print the nodes to console. I cant figure out how to write the nodes to the new XML in XML structure using the method's recursion.
i'm a noob to XML? am i missing something ?
XML EXAMPLE
<node clasification= some data about the node">
<dimension = some sort of info layer>
<children>
<node clasification= some data about the node">
<dimension = some sort of info layer>
<children>
<node clasification= some data about the node">
<dimension = some sort of info layer>
</children>
</node>
<node clasification= some data about the node">
<dimension = some sort of info layer>
</children>
</node>
</children>
</node>
</children>
</node>
my code is based on this:
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../Employees.xml");
XmlNode root = doc.SelectSingleNode("*");
ReadXML(root);
}
private static void ReadXML(XmlNode root)
{
if (root is XmlElement)
{
DoWork(root);
if (root.HasChildNodes)
ReadXML(root.FirstChild);
if (root.NextSibling != null)
ReadXML(root.NextSibling);
}
else if (root is XmlText)
{}
else if (root is XmlComment)
{}
}
private static void DoWork(XmlNode node)
{
if (node.Attributes["Code"] != null)
if(node.Name == "project" && node.Attributes["Code"].Value == "Orlando")
Console.WriteLine(node.ParentNode.ParentNode.Attributes["Name"].Value);
}
Please Help :)

i found a way to do it in a recursive way.
i cant share the final code because it is on a closed network with no access to the internet, but basically, i created a new node on the beginning of readxml , and returned it at the end, so i cant send the child object to it recursively...
something like that (pseudo code)
private static XmlNode ReadXML(XmlNode root)
{
XmlNode tmp = new XmlNode;
// pupulate the node attributes
tmp = DoWork(root);
foreach (xmlnode childnode in node.childnodes)
{
if (check if you want this node)
{
XmlNodeList tmplist = childnode.childnodes;
if (tmplist.HasChildNodes)
{
tmp.newnode = new ChildNode[tmplist.count];
for (i=0;i<tmpnode.count;i++)
{
tmp.tmpnode[i] = ReadXML(tmplist.item(i));
}
}
else
{}
}
}
now its working fine

Related

Get data from xml

I want get <SNILS> node:
<ArrayOfEmployee xmlns=""
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" version="2.0.11"
formatVersion="2.0" system="ARM">
<Employee>
<AdditionalLaborAgreement i:nil="true" /> <CertificateEducationList>
<Document> <SNILS>1111111111111</SNILS>
</Document>
</CertificateEducationList>
</Employee>
Code:
XmlNodeList nodes = xml.GetElementsByTagName("Employee");
XmlNode node = xml.SelectSingleNode("SVED_PR_GS/ZGLV/FILENAME");
int Count = 2;
foreach (XmlNode n in nodes)
{
XmlNode smr_vsi = n.SelectSingleNode("SNILS");
Console.WriteLine(n.SelectSingleNode(smr_vsi.InnerText));
}
Error: Console.WriteLine(n.SelectSingleNode(smr_vsi.InnerText));
The object reference does not point to an instance of the object.
Your XML is malformed. Its missing </ArrayOfEmployee> element.
You can get the desired node in one of the 2 ways:
// Provide full XPath
XmlNode smr_vsi = n.SelectSingleNode("CertificateEducationList/Document/SNILS");
//Provide find on any path hint.
XmlNode smr_vsi = n.SelectSingleNode("//SNILS");
But, do check for null before using it:
if(smr_vsi != null)
Console.WriteLine(smr_vsi.InnerText);

Reading xml file on some condition

I want to use a xml file which is as below
<?xml version="1.0" encoding="utf-8" ?>
<pages>
<page name="Default.aspx">
<method name="Login_click">
<message code="0" description="this is a test description">
<client code="0000000000" description="this is a description for clent 0000000000" />
</message>
</method>
</page>
</pages>
Now I want to create a function like below
public static string GetAppMessage(string pageName, string methodName, string clientCode, string code)
{
var xmlFile = HttpContext.Current.Server.MapPath("~/App_Data/Theme.xml");
var doc = new XmlDocument();
doc.Load(xmlFile);
if (string.IsNullOrEmpty(clientCode))
{
//go to node who page name to pageName
//and read the vlue
}
else
{
//read for a particular client code
}
}
How can I do this.
Edit 1
Do I need to loop through each node or can I reach to a particular node directly and find the decedent nodes.
like below
foreach (XmlNode chldNode in doc.ChildNodes)
{
....
Instead of XmlDocument you can use XDocument and LINQ to xml:
var xmlFile = HttpContext.Current.Server.MapPath("~/App_Data/Theme.xml");
XDocument xmlDoc = XDocument.Load(xmlFile);
var xmlPage = (from page in xmlDoc.Descendants()
where page.Name.LocalName == "page"
&& page.Attribute("name").Value == pageName
select page).FirstOrDefault();
if (xmlPage != null)
{
//do what you need
}
When you use XmlDocument and you know how the XML-file will look like (I mean you know the names of the nodes where the information is inside) then you could do something like this:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc["NameOfRootNode"];
if (root != null)
{
//For nodes you just need to bypass to get to another subnode:
XmlNode node = root.SelectSingleNode("nameOfAnotherNode");
//For nodes you actually want to do something with, like read text, attribute etc.
if (node != null && node.SelectSingleNode("nameOfOneMoreNode") != null)
{
var xmlElement = node["nameOfOneMoreNode"];
if (xmlElement != null)
{
//Read from the xmlElement you selected and do something with it...
}
}
//...
}
With SelectSingleNode or SelectNodes you can maneuver to a specific known node and can read the InnerText or an Attribute.
You can use XPath to get <page> element by it's name attribute, for example :
string xpath = "//page[#name='{0}']";
XmlNode page = doc.SelectSingleNode(string.Format(xpath, pageName));
//and read the vlue
Basically, above XPath look for <page> element having name attribute equals pageName parameter.

How to parse nested XML in C#

I'm working with an API and retrieving the data in XML. Here's my XML:
<RTT>
<AgencyList>
<Agency Name="Caltrain" HasDirection="True" Mode="Rail">
<RouteList>
<Route Name="BABY BULLET" Code="BABY BULLET">
<RouteDirectionList>
<RouteDirection Code="SB2" Name="SOUTHBOUND TO TAMIEN">
<StopList>
<Stop name="Sunnyvale Caltrain Station" StopCode="70222">
<DepartureTimeList/>
</Stop>
</StopList>
</RouteDirection>
<RouteDirection Code="NB" Name="NORTHBOUND TO SAN FRANCISCO">
<StopList>
<Stop name="Sunnyvale Caltrain Station" StopCode="70221">
<DepartureTimeList>
<DepartureTime>69</DepartureTime>
</DepartureTimeList>
</Stop>
</StopList>
</RouteDirection>
</RouteDirectionList>
</Route>
<Route Name="LIMITED" Code="LIMITED">...</Route>
<Route Name="LOCAL" Code="LOCAL">...</Route>
</RouteList>
</Agency>
</AgencyList>
</RTT>
Not every DepartureTimeList will have a DepartureTime child node. Here's what I got so far, which retrieves the Route name:
List<string> trainType = new List<string>();
XDocument doc = XDocument.Load("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=0f01ac4a-bc16-46a5-8527-5abc79fee435&agencyName=Caltrain&stopName=" + DropDownList1.SelectedItem.Text.ToString());
doc.Save("times.xml");
string feed = doc.ToString();
XmlReader r = XmlReader.Create(new StringReader(feed));
r.ReadToFollowing("RouteList");
if (r.ReadToDescendant("Route"))
{
do
{
trainType.Add(r.GetAttribute("Name"));
} while (r.ReadToNextSibling("Route"));
}
I'm mostly interested in the departure time (if it exists) and I've been struggling all afternoon to try and parse it.
Try this... Hopefully this will do it.
XmlDocument doc = new XmlDocument();
doc.Load("xml path");
XmlNode node = doc.SelectSingleNode("/RTT");
foreach (XmlNode nodes in node.SelectNodes(
"/AgencyList/Agency Name/RouteList/Route"))
{
trainType.Add(r.GetAttribute("Name"));
XmlNode s = nodes.SelectSingleNode("Route Name/RouteDirectionList/RouteDirection Code/StopList/Stop");
if (s != null && s["DepartureTimeList"].HasChildNodes)
{
// do stuff here
}
}

Split xml file based on nodes

I have an XML doc which looks roughly like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
<Single>
<Id>60000</Id>
<Type>Activate</Type>
<Payload>
<ns3:Activation>
<ns3:Parent>
<ns3:TypeId>113</ns3:TypeId>
<ns3:TypeName>TestApplication</ns3:TypeName>
</ns3:Parent>
<ns3:Children>
<ns3:Child>
<ns3:Key>someKey</ns3:Key>
<ns3:ChildTypeName>BadAppType1</ns3:ChildTypeName>
</ns3:Child>
<ns3:Child>
<ns3:Key>someOtherKey</ns3:Key>
<ns3:ChildTypeName>GoodAppType1</ns3:ChildTypeName>
</ns3:Child>
</ns3:Children>
</ns3:Activation>
</Payload>
</Single>
</Multiple>
If the file contains multiple "Child" nodes i would like to split it into more files, 1 file for each existing child node. Something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Multiple xmlns:ns2="someNs2" xmlns="someGenericNs" xmlns:ns4="someNs4" xmlns:ns3="someNs3">
<Single>
<Id>60000</Id>
<Type>Activate</Type>
<Payload>
<ns3:Activation>
<ns3:Parent>
<ns3:TypeId>113</ns3:TypeId>
<ns3:TypeName>TestApplication</ns3:TypeName>
</ns3:Parent>
<ns3:Children>
<ns3:Child>
<ns3:Key>someOtherKey</ns3:Key>
<ns3:ChildTypeName>GoodAppType2</ns3:ChildTypeName>
</ns3:Child>
</ns3:Children>
</ns3:Activation>
</Payload>
</Single>
</Multiple>
And then a new XmlDoc containing the other "Child" node. Can this be achieved through LINQ?
My code so far is:
private bool HasMoreThanOneChild(string xml) {
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
if (doc.GetElementsByTagName("ns3:Child").Count > 1)
{
return true;
}
return false;
}
public List<string> DoSomething(XmlDocument doc){
if(HasMoreThanOneChild(doc.InnerXml))
return Split(doc);
}
I returned a List there because I'm more interested in the "InnerXML" once the doc is split. But I am at a loss of how to implement this split, if it is possible.
XNamespace ns="someNs3";
//get all Child elements
var childElements=doc.Descendants().Elements(ns+"Child").ToList();
//remove those child elements
doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());
int i=1;
foreach(var child in childElements)
{
//add that child to children element
doc.Descendants().Elements(ns+"Children").First().Add(child);
//save it to new file!
doc.Save("file"+i+".xml");
doc.Descendants().Elements(ns+"Child").ToList().ForEach(x=>x.Remove());
i++;
}

C# remove attribute from root node

I tries to parse a XML file (get it from Dependacy Graph in VS 2012).
Here is sample of my .xml file
<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml">
<Nodes>
<Node Id="#101" Category="CodeSchema_ProjectItem" FilePath="$(ProgramFiles)\windows kits\8.0\include\um\unknwnbase.h" Label="unknwnbase.h" />
<Node Id="#103" Category="CodeSchema_ProjectItem" FilePath="$(ProgramFiles)\windows kits\8.0\include\shared\wtypesbase.h" Label="wtypesbase.h" />
in here, I needs to remove attribute "xmlns" from DirectedGraph.
here's my source to remove this
XmlNodeList rootNode = xmlDoc.GetElementsByTagName("DirectedGraph");
foreach (XmlNode node in rootNode)
{
node.Attributes.RemoveNamedItem("xmlns");
}
but this code doesn't work. If I don't delete this I can't select node like
XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes("/DirectedGraph/Nodes/Node");
What should I do?
If you like you can work with the namespace instead of removing the declaration:
var xml = #"<?xml version=""1.0"" encoding=""utf-8""?>
<DirectedGraph xmlns=""http://schemas.microsoft.com/vs/2009/dgml"">
<Nodes>
<Node Id=""#101"" Category=""CodeSchema_ProjectItem"" FilePath=""$(ProgramFiles)\windows kits\8.0\include\um\unknwnbase.h"" Label=""unknwnbase.h"" />
<Node Id=""#103"" Category=""CodeSchema_ProjectItem"" FilePath=""$(ProgramFiles)\windows kits\8.0\include\shared\wtypesbase.h"" Label=""wtypesbase.h"" />
</Nodes>
</DirectedGraph>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("d", "http://schemas.microsoft.com/vs/2009/dgml");
var nodes = doc.DocumentElement.SelectNodes("/d:DirectedGraph/d:Nodes/d:Node", manager);
Console.WriteLine(nodes.Count);
Use:
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
foreach (XAttribute attribute in xmlDocument.Attributes())
xElement.Add(attribute);
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}
Taken from: How to remove all namespaces from XML with C#?.
You might also want to check out: XmlSerializer: remove unnecessary xsi and xsd namespaces.

Categories

Resources