xml c# read and write helper - c#

I have the following examples, and the c# is just my draft. Can you show me how to call the xml file and read there so I can get the value
public static ArrayList GetLocationLiabilityAmount()
{
ArrayList al = new ArrayList();
string selectedValue = Library.MovieClass.generalLibailityLocationLiability;
if (!String.IsNullOrEmpty(selectedValue))
{
if (option from xml == selectedValue)
{
al.Add(minvalue);
al.Add(maxvalue);
}
return al;
}
else
{
return null;
}
}
XML:
<?xml version="1.0" encoding="utf-8" ?>
<AccidentMedicalCoverage>
<coverage option="1" value="10000" showvalue="$10,000 per person"></coverage>
<coverage option="2" value="25000" showvalue="$25,000 per person"></coverage>
<coverage option="3" value="50000" showvalue="$50,000 per person"></coverage>
</AccidentMedicalCoverage>

The question is not too clear but this is what I assume you want:
Given an option if you want to get the value from the XML this is one way you can do it:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("c:\\xmlfile\\coverage.xml");
// Select the node with option=1
XmlNode node = xDoc.SelectSingleNode("/AccidentMedicalCoverage/coverage[#option='1']");
// Read the value of the Attribute 'value'
var value = node.Attributes["value"].Value;

I prefer linq to Xml. There are two ways given to get the data into an XDocument shown below and then a basic query into the data
//var xml = File.ReadAllText(#"C:\data.xml");
var xml = GetFile();
//var xDoc = XDocument.Load(#"C:\data.xml"); Alternate
var xDoc = XDocument.Parse(xml);
var coverages = xDoc.Descendants("coverage");
coverages.Select (cv => cv.Attribute("showvalue").Value)
.ToList()
.ForEach(showValue => Console.WriteLine (showValue));
/* Output
$10,000 per person
$25,000 per person
$50,000 per person
*/
...
public string GetFile()
{
return #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<AccidentMedicalCoverage>
<coverage option=""1"" value=""10000"" showvalue=""$10,000 per person""></coverage>
<coverage option=""2"" value=""25000"" showvalue=""$25,000 per person""></coverage>
<coverage option=""3"" value=""50000"" showvalue=""$50,000 per person""></coverage>
</AccidentMedicalCoverage>";
}

Related

Extract data from xml string

Let's say I have an xml string:
<?xml version="1.0" encoding="UTF-8"?>
<Return version="1.0">
<File>1</File>
<URL>2</URL>
<SourceUUID>1191CF90-5A32-4D29-9F90-24B2EXXXXXX0</SourceUUID>
</Return>
and I want to extract the value of SourceUUID, how?
I tried:
XDocument doc = XDocument.Parse(xmlString);
foreach (XElement element in doc.Descendants("SourceUUID"))
{
Console.WriteLine(element);
}
If all you want is the content of the SourceUUID element, and there's only going to be 1 in the XML, you can do this:
XDocument doc = XDocument.Parse(xmlString);
var value = doc.Descendants("SourceUUID").SingleOrDefault()?.Value;
If there are going to be more than one, you can do this:
var values = doc.Descendants("SourceUUID").Select(x => x.Value);
This gives you an enumerable of strings that are the text values of the elements.

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".**

How to get element by element from XmlDocument

I was able to get my first element from this list
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetITARListResponse xmlns="http://tempuri.org/">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d/legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
by writing
XDocument result = new XDocument();
result = ExportControl.ResultXML;
var codes = HttpContext.Current.Server.MapPath("~/XML_Templates/codes.xml");
dynamic root = new ExpandoObject();
XmlToDynamic.Parse(root, xDoc.Elements().First());
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code").First();
which get the the first code "dsd". But what if I want a foreach and get all the code? or what if I want a certain code ? for example
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code")[2]
.Net framework provides a set of tools you can use to move over an XML file:
XmlReader: this class reads xml files in hierarchical manner and only forward
without cached.
XmlWriter: this class writes in xml files only forward without
cached.
XmlDocument: it helps you to navigate and edit small documents as it's
slower than XmlReader/Writer. It uses XmlNode objects to move through your document and perform changes (attributes). After editing your you can save. A nifty way to navigate is by using XPath
(query language for xml).
XPathNvigator: class offers an easy way to navigate through an XML document.
In your case, my recommendation is to implement a couple of methods one for iteration and one for location of a particular node with XmlReader and XPath respectively.
Update: the XML example is malformed here:
<legislation>d/legislation>
This shows you an example to read the file:
using System;
using System.Xml;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
string xml = #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<soap:Body>
<GetITARListResponse xmlns=""http://tempuri.org/"">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d</legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
</GetITARListResult>
</GetITARListResponse>
</soap:Body>
</soap:Envelope>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var items = doc.GetElementsByTagName("ClassificationCode");
foreach (var item in items)
{
Console.WriteLine(((System.Xml.XmlElement)(item)).InnerText.ToString());
}
Console.ReadLine();
}
}
}
// OUTPUT
// dsdtotod
// dsdtatadsd
// code2dsdsdsd
Your code sample is returning the First item merely because you're calling First() method. In order to loop through all codes you can skip the call and you will get an IEnumerable. Then you can loop like this:
var codes = result.Descendants(XNamespace.Get("http://tempuri.org/") + "code");
foreach (var codeElement in codes)
{
Console.WriteLine(codeElement.Value);
}
// prints:
// dsd
// dsd
// code2
To access them by index you can use ElementAt like this:
var someCode = codes.ElementAt(1);
Console.WriteLine(someCode.Value); // prints dsd
Or you can filter by name like this:
var code2 = codes.Where(c => c.Value == "code2").First();
Console.WriteLine(code2.Value); // prints code2

C# XML How to retrive innerText of field by attribute?

Here is the XML sample:
<?xml version="1.0" ?>
<XMLScreen xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CX>80</CX>
<CY>24</CY>
<Formatted>true</Formatted>
<Field>
<Location position="1" left="1" top="0" length="69" />
<Attributes Base="226" Protected="false" FieldType="High" />
*SDC SCHEDULING CATEGORY UPDATE
</Field>
</XMLScreen>
I want to retrive the Inner text of each field based on its Location position.
What I have so far is:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myEm.CurrentScreenXML.GetXMLText());
XmlNodeList fields = xmlDoc.GetElementsByTagName("Field");
MessageBox.Show("Field spot: " + i + " Contains: " + fields[i].InnerText);
And I want to be able to just extract the inner text of the field by passing in a number of the location position. So if I say foo[i] I want to be able to get the innertext
*SDC SCHEDULING CATEGORY UPDATE
You should use a xpath search query :
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
int nodeId = 4;
XmlNode node = xmlDoc.SelectSingleNode(String.Format(#"//Location[#position='{0}']", nodeId));
if (node != null)
{
String field = node.ParentNode.InnerText;
}
Something like that, with XDocument instead of XmlDocument (well, if you're not in .net 3.5 or higher, we'll have a problem).
private string GetTextByLocationId(XDocument document, int id)
{
var field = document.Descendants("Field").FirstOrDefault(m => m.Element("Location").Attribute("position").Value == id.ToString());
if (field == null) return null;
return field.Value;
}
and usage
var xDocument = XDocument.Load(<pathToXmlFile or XmlReader or string or ...>);
var result = GetTextByLocationId(xDocument, 1);
EDIT
or if you want a dictionary with :key = position / value = text
private static Dictionary<int, string> ParseLocationAndText(XDocument document)
{
var fields = document.Descendants("Field");
return fields.ToDictionary(
f => Convert.ToInt32(f.Element("Location").Attribute("position").Value),
f => f.Value);
}
Try,
XElement root = XElement.Parse(myEm.CurrentScreenXML.GetXMLText());
XElement field = root.XPathSelectElement(
string.Format("Field[Location/#position='{0}']", 1));
string text = field.Value;
You will need to use the following using to use XPath with XElements.
using System.Xml.XPath;

Categories

Resources