Parse dynamically created XML document in c# - c#

I have to parse an XML document like this:
<?xml version="1.0" encoding="utf-8" ?>
<DialsList>
<Dial>
<Id>123</Id>
<Source>SQL</Source>
<Server>myServer</Server>
<Database>myDB</Database>
<UserName>myUserName</UserName>
<Password>myPassword</Password>
<TableName>myTable</TableName>
<Query>GetAvgForUser</Query>
<Parameters>
<Param1>
<Name>#DialId</Name>
<Type>Int</Type>
</Param1>
<Param2>
<Name>#UserId</Name>
<Type>String</Type>
</Param2>
</Parameters>
</Dial>
<Dial>
<Id>789</Id>
<Source>SQL</Source>
<Server>someServer</Server>
<Database>someDB</Database>
<UserName>someUser</UserName>
<Password>somePwd</Password>
<TableName>someTbl</TableName>
<Query>someQry</Query>
<Parameters>
<Param1>
<Name>#myParam</Name>
<Type>Int</Type>
</Param1>
<Param2>
<Name>#anotherParam</Name>
<Type>String</Type>
</Param2>
</Parameters>
</Dial>
</DialsList>
My application has to read this XML document and form the connection string as well as execute the query/storedprocedure specified in the <Query> node. I have no problems forming the connectionstring. Since it can be any stored procedure and it can have any number of parameters,I cannot figure out a way to parse this XML. The XML will be dynamic - parameters,parameter names,parameter type,etc. and it's not under my control. I have this code so far which works if there's only 1 Parameter(I'm assuming it will be "int"):
XDocument doc = XDocument.Load(xmlFileName);
string dialID = Convert.ToString(DialId);
XElement DialsDoc = (from xml2 in doc.Descendants("Dial") where xml2.Element("Id").Value == dialID select xml2).FirstOrDefault();
dialDataSrc.Source = DialsDoc.Element("Source").Value.ToString();
if (dialDataSrc.Source.ToString() == "SQL")
{
DataSource dialDataSrc = new DataSource();
dialDataSrc.MethodNameIndividual = string.Empty;
dialDataSrc.MethodNameGroup = string.Empty;
dialDataSrc.Server = DialsDoc.Element("Server").Value.ToString();
dialDataSrc.Database = DialsDoc.Element("Database").Value.ToString();
dialDataSrc.Username = DialsDoc.Element("UserName").Value.ToString();
dialDataSrc.Password = DialsDoc.Element("Password").Value.ToString();
dialDataSrc.TableName = DialsDoc.Element("TableName").Value.ToString();
dialDataSrc.Query = DialsDoc.Element("Query").Value.ToString();
dialDataSrc.Parameters = DialsDoc.Element("Parameters").Value.ToString();
}
I then form the connection string and execute the stored procedure.
How do I go about handling this when it's dynamic?

I can't figure out the Type of dialDataSrc but this should get you on your way:
var listOfparameters = DialsDoc
.Element("Parameters")
.Elements()
.Select(p => MyCreateParam((string) p.Element("Name"), (string) p.Element("Type")))
.ToList();
With a little helper method to figure out the DbType etc:
private SqlParameter MyCreateParam(string name, string _type) { ... }

Related

Get data from XML document using c#

I would like to get datas from a specific XML format. The XML document looks like that:
<MyXML>
<Sources>
<S1>www.example1.org</S1>
<S2>www.example2.org</S2>
</Sources>
<Books>
<Book id="1">
<Datas>
<Data name="Book_1_Name" tag="1111" />
<Data name="Book_2_Name" tag="2222" />
</Datas>
</Book >
<Book id="2">
<Datas>
<Data name="Book_1_Name" tag="3333" />
<Data name="Book_2_Name" tag="4444" />
</Datas>
</Book >
</Books>
My question is:
How can I get www.example1.org if I know S1?
How can I search "Book_1_name" from Book id=1?
I am using C# with XDocument like this:
XDocument.Load(_XML_path);
var node = _configXml.XPathSelectElement("MyXML/Books/Datas");
You can use XPath, About XPath see this : http://www.xfront.com/xpath/
var _configXml = XDocument.Load(_XML_path);
//how to get S1 element.
var s1 = _configXml.XPathSelectElement("MyXML/Sources/S1");
//how search
var search = _configXml.XPathSelectElements("//Book[#id='1']//Data[#name='Book_1_Name']");
Console.WriteLine(s1.Value);
Console.WriteLine(search.First().Attribute("tag").Value);
You should map the XML to a C# object. Then, you can use the following to get what you want:
XmlSerializer serializer = new XmlSerializer(typeof(MyXML));
var xml = (MyXML)serializer.Deserialize(new StringReader(XDocument.Load(_XML_path)));
var s1 = xml.Sources.S1;
If you want to stick with a XDocument, you can do the following
var books = doc.Element("MyXML").Element("Books");
var bookById = books.Elements("Book")
.Where(b => b.Attribute("id").Value == "1")
.Select(b => b.Element("Datas"));
In the first line, you are selecting the Books node (please note, in a real-world scenario, I'd add some checks here). In the following line you are first getting all Book sub-elements (books.Elements("Book")), checking them for the ID you are searching for (Where(b => b.Attribute("id").Value == "1")) and then select the data node from the respective book. You could re-write this to query syntax
var bookById = from book in books.Elements("Book")
let id = book.Attribute("id")
let datas = book.Element("Datas")
where id != null
&& datas != null
&& id.Value == "1"
select datas;
which is a bit longer, but easier to read.

How to Display the XML data to a GRIDVIEW

i have this following XML file
<personaldetails>
<name>ravi</name>
<id>1</id>
<branch>CSE</branch>
</personaldetails>
<professionaldetails>
<name>ravi</name>
<age>25</age>
<gender>male</gender>
</professionaldetails>
This is the sample data .so,now when i search with a name "ravi" in textbox it should display both tables i.e; personal and professional.how to do this using DATATABLE and bind it to GRIDVIEW.
Am doing this in ASP.NET using C#
How can we solve this issue using c#
or
can we do this using LINQ QUERIES
You might use Linq To XML with a valid XML. ie:
string sXML = #"<root>
<personaldetails>
<name>ravi</name>
<id>1</id>
<branch>CSE</branch>
</personaldetails>
<professionaldetails>
<name>ravi</name>
<age>25</age>
<gender>male</gender>
</professionaldetails>
</root>";
var prd = XElement
.Parse(sXML)
.Descendants()
.Where(xe => xe.Name=="professionaldetails" && (string)xe.Element("name") == "ravi")
.Select(p => new {
Name = (string)p.Element("name"),
Age = (int?)p.Element("age"),
Gender = (string)p.Element("gender")
}) ;

XML node name and attributes are not available

I have the following XML structure:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<values>
<bool key="Chapter_1.Boolean1.value">true</bool>
<string key="Chapter_1.Text1.value">abc</string>
<string key="Chapter_1.Text2.value">Inspection done (2)</string>
<number key="Chapter_1.Number1.value">128</number>
<number key="Chapter_1.Number2.value">34539718</number>
<number key="Chapter_1.Number3.value">3</number>
<datetime key="Chapter_2.Chapter_2_1.DateTime1.value">2020-06-02T09:00:00+03:00</datetime>
<datetime key="Chapter_2.Chapter_2_1.DateTime2.value">2016-02-05T00:00:00+02:00</datetime>
<string key="Chapter_3.Text4.value">52</string>
<string key="Chapter_3.Text5.value">22</string>
<number key="Chapter_3.Number6.value">34539718</number>
</values>
and the following C# code:
var settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
using (var xmlReader = new XmlTextReader(xmlFilePath))
{
while (xmlReader.Read())
{
var nodeName = xmlReader.Name;
var attrName = xmlReader.GetAttribute("key");
}
}
The problem is that the node name is empty and there are no attributes for the following keys:
Chapter_1.Text1.value
Chapter_1.Number1.value
Chapter_3.Text5.value
Anyone has any idea what could be the problem?
The code worked well here, I was able to access all xml tags and attributes.
Maybe you're confused because at each xmlReader.Read() it reads only one part of the tag. So to read all the tag with key "Chapter_1.Text1.value", first it reads a tag with name string and key "Chapter_1.Text1.value", then it reads something without a name, without a attribute, but with value "abc" and then it reads the tag closing with name string, but no attribute and no value.
It would be easier to use Xml to Linq :
var xml = XDocument.Load(__PATH_TO_XML__);
var values = xml.XPathSelectElements("/values/*")
.Select(x => new
{
Type = x.Name,
Key = x.Attribute("key"),
Value = x.Value
});
If you want to read the value as well, try this
using (var xmlReader = new XmlTextReader(#"yourxmlfile"))
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element)
{
var nodeName = xmlReader.Name;
var attrName = xmlReader.GetAttribute("key");
Console.WriteLine(nodeName);
Console.WriteLine(attrName);
}
if (xmlReader.NodeType==XmlNodeType.Text)
{
Console.WriteLine(xmlReader.Value);
}
}
}

how to print the innertext of an element based on attribute search of a particular node using LINQ?

**I have an XML like this-
<?xml version="1.0" encoding="UTF-8"?>
<Tool_Parent>
<tool name="ABCD" id="226">
<category>Centralized</category>
<extension_id>0</extension_id>
<uses_ids>16824943 16824944</uses_ids>
</tool>
<tool name="EFGH" id="228">
<category>Automated</category>
<extension_id>0</extension_id>
<uses_ids>92440 16824</uses_ids>
</tool>
</Tool_Parent>
Based on the id of tool i want to print the uses_ids value,i.e if i search for 228 i should get 92440 16824.
I had tried like-
var toolData = (from toolElement in doc.Descendants("tool")
select new Tool_poco
{
a_Name = tool.Attribute("name").Value,
a_Id = tool.Attribute("id").Value,
e_ExtensionId = tool.Element("extension_id").Value,
e_UsesIds =tool.Element("uses_parm_ids").Value
});
where Tool_poco is a poco class for tool node containing declaration for member variable.
Now I want to get information related to a particular tool id in toolData variable.How to do it?
Note: I have variable like-
searched_Tool_id = Tool_Id_txtBx.Text.ToString();
Please let me know a way through which i can modify my above query for toolData.**
You can modify your query as
Tool_poco toolData = (from el in xelement.Elements("Employee")
where (string)el.Attribute("id") == "226"
select new Tool_poco
{
a_Name = el.Attribute("name").Value,
a_Id = el.Attribute("id").Value,
e_ExtensionId = el.Element("Name").Value,
e_UsesIds = el.Element("uses_ids").Value
}).FirstOrDefault();
You could start by doing something like this once you have an XDocument object loaded and ready:
var xdoc = XDocument.Parse(
#"<?xml version=""1.0"" encoding=""utf-8""?>
<Tool_Parent>
<tool name=""ABCD"" id=""226"">
<category>Centralized</category>
<extension_id>0</extension_id>
<uses_ids>16824943 16824944</uses_ids>
</tool>
<tool name=""EFGH"" id=""228"">
<category>Automated</category>
<extension_id>0</extension_id>
<uses_ids>92440 16824</uses_ids>
</tool>
</Tool_Parent>");
var root = xdoc.Root; // Got to have that root
if (root != null)
{
var id228query = (from toolElement in root.Elements("tool")
where toolElement.HasAttributes
where toolElement.Attribute("id").Value.Equals("228")
let xElement = toolElement.Element("uses_ids")
where xElement != null
select xElement.Value).FirstOrDefault();
Console.WriteLine(id228query);
Console.Read();
}
Output: 92440 16824
**Note: In reference to your example, one possible reason it was not working
for you could be that your xml references an element with name "uses_ids",
however, your query references an element with a similar, but not exact,
spelling with name "uses_parm_ids".**

Before inserting new element in xml check if exists the value

I have below a xml file with the below format:
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Countries>
<country>India</country>
<country>USA</country>
<country>UK</country>
</Countries>
</Root>
string newCountry="UAE"
I want to insert this "UAE" country to the above xml file, before that I want to check whether "UAE" is already exists in the xml. If not exists then only want to insert otherwise no operation. How can I do this?
Like this:
XDocument xml = XDocument.Load("path_to_file");
string newCountry = "UAE";
XElement countries = xml.Descendants("Countries").First();
XElement el = countries.Elements().FirstOrDefault(x => x.Value == newCountry);
if (el == null)
{
el = new XElement("country");
el.Value = newCountry;
countries.Add(el);
}
//Console.WriteLine(countries.ToString());
The easiest way would probably be to read the xml into C# objects, check for the existance of UAE, potentially add it, and write the objects back to XML.

Categories

Resources