Extracting a Value From an XML File in C# - c#

Can someone help me out with extracting the "Value Y" of 4.247 in this XML file. I've searched through previous threads but can't seem to apply them to this structure.
<Chart1>
<Chart1_SeriesGroup_Collection>
<Chart1_SeriesGroup Label="MP_Trend_Data\MP_Prog/MP_DAILY_FLOW">
<Chart1_CategoryGroup_Collection>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:59:19 AM">
<Value X="0001-01-01T10:30:00+10:30"/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label="12/24/2017 1:58:19 AM">
<Value Y="4.24700021743774" X="2017-12-24T12:28:19.333+10:30"/>
</Chart1_CategoryGroup>
code I've tried:
var xDoc = XDocument.Load("C:\\attachment\\Flow.xml");
XmlDocument xml = new XmlDocument();
xml.LoadXml(xDoc.ToString());
XmlNodeList xnList = xml.SelectNodes("/Chart1/Chart1_SeriesGroup_Collection/Chart1_SeriesGroup[#Label='MP_Trend_Data\\MP_Prog/MP_DAILY_FLOW']");
foreach (XmlNode Node in xnList)
{
XmlNodeList ynList = Node.SelectNodes("/Chart1_CategoryGroup_Collection/Chart1_CategoryGroup/Value");
foreach (XmlNode Node2 in ynList)
{
textBox1.Text = Node2.Attributes["Y"].Value.ToString();
}
}

var xmlStr = #"
<Chart1>
<Chart1_SeriesGroup_Collection>
<Chart1_SeriesGroup Label='MP_Trend_Data\MP_Prog/MP_DAILY_FLOW'>
<Chart1_CategoryGroup_Collection>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:59:19 AM'>
<Value X='0001-01-01T10:30:00+10:30'/>
</Chart1_CategoryGroup>
<Chart1_CategoryGroup Label='12/24/2017 1:58:19 AM'>
<Value Y='4.24700021743774' X='2017-12-24T12:28:19.333+10:30'/>
</Chart1_CategoryGroup>
</Chart1_CategoryGroup_Collection>>
</Chart1_SeriesGroup>
</Chart1_SeriesGroup_Collection>
</Chart1>";
var xml = XElement.Parse(xmlStr);
var firstY = xml.Descendants("Value").FirstOrDefault(v => v.Attribute("Y") != null);
if (firstY != null)
{
var y = firstY.Attribute("Y").Value;
}

Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("Chart1_CategoryGroup").Where(x => x.Element("Value").Attribute("Y") != null).Select(x => new {
label = (DateTime)x.Attribute("Label"),
y = (double)x.Element("Value").Attribute("Y"),
x = (DateTime)x.Element("Value").Attribute("X")
}).ToList();
}
}
}

Related

C# - Convert XML elements to key value pair in a concatenated string

I Have below XML as an input
<Details>
<PAY>
<Mode>xyz</Mode>
<REMARKS>no</REMARKS>
</PAY>
<data>
<Customer>
<Participant>
<Participant1FirstName>aaaa</Participant1FirstName>
<Participant1LastName>zzzz</Participant1LastName>
</Participant>
<Participant>
<Participant2FirstName>bbbb</Participant2FirstName>
<Participant2LastName>yyyy</Participant2LastName>
</Participant>
<Participant>
<Participant3FirstName>cccc</Participant3FirstName>
<Participant3LastName>xxxx</Participant3LastName>
</Participant>
</Customer>
</data>
</Details>
Desired output :
String strOutput = "|Participant1FirstName = aaaa|Participant1LastName = zzzz|; |Participant2FirstName = bbbb|Participant2LastName = yyyy|; |Participant3FirstName = cccc|Participant3LastName = xxxx|"
SO far, I have tried parsing XML string into XDocument, and thinking of using Linq to get the desired output, but don't know the way forward.
Input :
string strXML = "<Details> <PAY> <Mode>xyz</Mode> <REMARKS>no</REMARKS> </PAY> <data> <Customer> <Participant> <Participant1FirstName>aaaa</Participant1FirstName> <Participant1LastName>zzzz</Participant1LastName> </Participant> <Participant> <Participant2FirstName>bbbb</Participant2FirstName> <Participant2LastName>yyyy</Participant2LastName> </Participant> <Participant> <Participant3FirstName>cccc</Participant3FirstName> <Participant3LastName>xxxx</Participant3LastName> </Participant> </Customer> </data></Details>"
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(strXML);
Edit
Any way I can get desired output from below XML ?
<Details>
<PAY>
<Mode>xyz</Mode>
</PAY>
<data>
<Customer>
<Participant>
<Participant1>
<Participant1FirstName>aaaa</Participant1FirstName>
<Participant1LastName>zzzz</Participant1LastName>
</Participant1>
<Participant2>
<Participant2FirstName>bbbb</Participant2FirstName>
<Participant2LastName>yyyy</Participant2LastName>
</Participant2>
<Participant>
</Customer>
</data>
</Details>
You could do it like this
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(strXML);
var values = xdoc.Descendants("Participant").Elements()
.Select(x => x.Name + " = " + x.Value).ToList();
string result = string.Join("|", values);
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<Particapant> participants = doc.Descendants("Participant").Select(x => new Particapant()
{
firstName = (string)x.Elements().FirstOrDefault(),
lastName = (string)x.Elements().LastOrDefault()
}).ToList();
string output = string.Join(";", participants
.Select(x => string.Format("|ParticipantFirstName = {0}|ParticipantLastName = {1}|", x.firstName, x.lastName)));
}
}
public class Particapant
{
public string firstName { get; set; }
public string lastName { get; set; }
}
}

How can I iterate through xml and remove the duplicates? C#

I've created a program to retrieve data from a database and convert it to XML. It returns a list of 24 objects that I created:
<?xml version = "1.0" encoding = "utf-16"
<ArrayOfData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Data>
<interval>01</interval>
<num>12345</num>
<id>ABC123 </id>
<type>LETTER</type>
<party>1</party>
<amount>-10</amount>
</Data>
<Data>
<interval>02</interval>
<num>12345</num>
<id>ABC123 </id>
<type>LETTER</type>
<party>1</party>
<amount>-11</amount>
</Data>
<Data>
<interval>03</interval>
<num>12345</num>
<id>ABC123 </id>
<type>LETTER</type>
<party>1</party>
<amount>-12</amount>
</Data>
<Data>
<interval>04</interval>
<num>12345</num>
<id>ABC123 </id>
<type>LETTER</type>
<party>1</party>
<amount>-13</amount>
</Data>
Basically the data goes on with incrementing intervals by 1 up to 24.
What I am looking for is to look for what is duplicate and just concatenate them into one child, and then to look for what differs and create a different element for it but keep them everything in one child. How can I write the XML to do this? Example:
<Data>
<num>12345</num>
<id>ABC123 </id>
<type>LETTER</type>
<party>1</party>
<details>
<interval>01</interval>
<amount>-10</amount>
</details>
<details>
<interval>02</interval>
<amount>-11</amount>
</details>
<details>
<interval>03</interval>
<amount>-12</amount>
</details>
<details>
<interval>04</interval>
<amount>-13</amount>
</details>
</Data>
I want to create a function that returns this XML. The parameters in my current function now are just there to get the data using those parameters. Here's how I generate the XML now:
public IActionResult CreateXML(int num, int id)
{
string xml = "";
var temp = (from x in Context.InternalData.AsNoTracking() where (x.num == num && x.id == id) select x).ToList();
using (var stringwrite = new Utf8StringWriter())
{
var serializer = new XmlSerializer(temp.GetType());
serializer.Serialize(stringwrite, temp);
xml = stringwrite.ToString();
StringReader reader = new StringReader(stringwriter.ToString());
reader.ReadLine(); //skips the UTF-16 header
XDocument doc = XDocument.Load(reader);
XElement arrayOfData = doc.Root;
List<XElement> newData = new List<XElement>();
var parties = doc.Descendants("Data")
.GroupBy(x => (string)x.Element("id"))
.ToList();
foreach (var party in parties)
{
XElement template = party.First();
List<XElement> details = party.Select(x => new XElement("details", new object[] {
new XElement("interval", (string)x.Element("interval")),
new XElement("amount", (string)x.Element("amount"))
})).ToList();
template.Element("interval").Remove();
template.Element("amount").Remove();
template.Add(details);
newData.Add(template);
}
arrayOfData.ReplaceAll(newData);
return Ok(arrayOfData);
}
}
I am new to this and have never worked with generating XML before. Any help would be greatly appreciated!
Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
StringWriter stringwriter = new StringWriter();
stringwriter.Write(xml);
StringReader reader = new StringReader(stringwriter.ToString());
reader.ReadLine(); //skips the UTF-16 header
XDocument doc = XDocument.Load(reader);
XElement arrayOfData = doc.Root;
List<XElement> newData = new List<XElement>();
var parties = doc.Descendants("Data")
.GroupBy(x => (string)x.Element("id"))
.ToList();
foreach (var party in parties)
{
XElement template = party.First();
List<XElement> details = party.Select(x => new XElement("details", new object[] {
new XElement("interval", (string)x.Element("interval")),
new XElement("amount", (string)x.Element("amount"))
})).ToList();
template.Element("interval").Remove();
template.Element("amount").Remove();
template.Add(details);
newData.Add(template);
}
arrayOfData.ReplaceAll(newData);
}
}
}

How to extract data from SOAP response in C#

I have following SOAP response. Using my code I could only extract one element.
and then it throws null reference exception error.
How do I extract Item->key and Item->value in to my dictionary in the function in C#?
Following is a part of the function which i use to extract data from.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP- ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:callResponse>
<callReturn SOAP-ENC:arrayType="ns2:Map[2]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">state</key>
<value xsi:type="xsd:string">processing</value>
</item>
<item>
<key xsi:type="xsd:string">status</key>
<value xsi:type="xsd:string">processing</value>
</item>
<item>
<key xsi:type="xsd:string">protect_code</key>
<value xsi:type="xsd:string">ba8dd7</value>
</item>
<item>
<key xsi:type="xsd:string">shipping_firstname</key>
<value xsi:type="xsd:string">Roshan</value>
</item>
<item>
<key xsi:type="xsd:string">billing_name</key>
<value xsi:type="xsd:string">Roshan India</value>
</item>
<item>
<key xsi:type="xsd:string">shipping_name</key>
<value xsi:type="xsd:string">Roshan India</value>
</item>
<item>
<key xsi:type="xsd:string">order_id</key>
<value xsi:type="xsd:string">2</value>
</item>
</item>
</callReturn>
</ns1:callResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The codes,
try
{
XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
xdoc.LoadXml(content); // --------> THIS IS WHERE I PASS SOAP RESPONSE
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("SOAP-ENV:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("//item");
int nodes = xNodelst.Count;
dynamic item;
Dictionary<string, string> items = new Dictionary<string, string>();
foreach (XmlNode xn in xNodelst)
{
XmlNode itemnode = xn["item"];
if (itemnode != null) {
string key = itemnode["key"].InnerText;
string value = itemnode["value"].InnerText;
items.Add(key, value);
}
}
var ss = items;
return "";
}
catch (Exception e) {
return e.Message;
}
The xml has two level of the tag item. So you have to make sure you treat each level of the item tag separately. I used xml linq to get data and put results into a dictionary.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
XElement firstItem = doc.Descendants(ns + "item").FirstOrDefault();
Dictionary<string, string> dictItems = firstItem.Descendants(ns + "item")
.GroupBy(x => (string)x.Element("key"), y => (string)y.Element("value"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
I finally managed to make this work. I added all the namespaces in namespace manager variable. And used xPath to navigate through the XML as below,
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("//item[#xsi:type]",nsmgr);
This helped me to resolve my problem. I'm posting this here in case if anyone having similar issue.
Cheers!
string soapString = #"<soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:Magento""><soapenv:Header/><soapenv:Body><urn:call soapenv:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><sessionId xsi:type=""xsd:string"">"+ssid +#"</sessionId><resourcePath xsi:type=""xsd:string"">sales_order.list</resourcePath><args xsi:type=""xsd:anyType""></args></urn:call></soapenv:Body></soapenv:Envelope>";
HttpResponseMessage response = await PostXmlRequest("http://localhost/M1/api.php", soapString);
string content = await response.Content.ReadAsStringAsync();
XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
xdoc.LoadXml(content);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("ns1", "urn:Magento");
nsmgr.AddNamespace("ns2", "http://xml.apache.org/xml-soap");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("//item[#xsi:type]",nsmgr);
int nodes = xNodelst.Count;
Dictionary<int, IDictionary> items = new Dictionary<int, IDictionary>();
int n = 0;
foreach (XmlNode xn in xNodelst)
{
XmlNode itemnode = xn;
Dictionary<string, string> itemData = new Dictionary<string, string>();
foreach (XmlNode xn1 in itemnode)
{
string key = xn1["key"].InnerText;
string value = xn1["value"].InnerText;
itemData.Add(key, value);
}
items.Add(n, itemData);
n++;
}
return items;

How to group XML nodes based on their values in C#

I have my XML as:
<root>
    <element>
        <id>1</id>
        <group>first</group>
    </element>
    <element>
        <id>2</id>
        <group>second</group>
    </element>
    <element>
        <id>3</id>
        <group>first</group>
    </element>
</root>
Is there anyway we can group the nodes with same values like this:
<root>
    <groups name="first">
<element>
        <id>1</id>
        <group>first</group>
     </element>
<element>
        <id>3</id>
        <group>first</group>
    </element>
</groups>
   <groups name="second"><element>
       <id>2</id>
        <group>second</group>
    </element>
</groups>
</root>
Is there a way to group it based on same node values?
I just tested the code below and it matches your results.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<root>" +
"<element>" +
"<id>1</id>" +
"<group>first</group>" +
"</element>" +
"<element>" +
"<id>2</id>" +
"<group>second</group>" +
"</element>" +
"<element>" +
"<id>3</id>" +
"<group>first</group>" +
"</element>" +
"</root>";
XDocument doc = XDocument.Parse(xml);
var groups = doc.Descendants("element")
.GroupBy(x => (string)x.Element("group"))
.ToList();
XElement newXml = new XElement("root");
foreach(var group in groups)
{
newXml.Add(new XElement("groups", new object[] {
new XAttribute("name", group.Key),
group
}));
}
}
}
}
XPath approach
string val= "first";
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Xml/YourXML.xml"));
string text = string.Empty;
XmlNodeList xnl = doc.SelectNodes("/root/groups ");
foreach (XmlNode node in xnl)
{
text = node.Attributes["name"].InnerText;
if (text == val)
{
XmlNodeList xnl = doc.SelectNodes(string.Format("/root/groups [#name='{0}']/element", val));
foreach (XmlNode node2 in xnl )
{
text = text + "<br>" + node2["id"].InnerText;
text = text + "<br>" + node2["group"].InnerText;
}
}
Response.Write(text);
}
or
var nodes = (from n in xml.Descendants("element").
Where(r => r.Parent.Attribute("name").Value == "first")
select new
{
id = (string)n.Element("id").Value,
group = (string)n.Element("group").Value
}).ToList();
Group the elements and build up a new document placing each group in a new <groups> element.
var newDoc = new XDocument(
new XElement("root",
from e in doc.Descendants("element")
group e by (string)e.Element("group") into g
select new XElement("groups",
new XAttribute("name", g.Key),
g
)
)
);
I had to try this in VB. My Group By skills need a lot of practice.
Using this test data
Dim myXML As XElement
myXML = <root>
<element>
<id>1</id>
<group>first</group>
</element>
<element>
<id>2</id>
<group>second</group>
</element>
<element>
<id>3</id>
<group>first</group>
</element>
</root>
this seems to work
Dim newXML As XElement = <root></root>
newXML.Add(From el In myXML.Elements
Order By el.<group>.Value
Group By gn = el.<group>.Value Into g = Group
Select New XElement("Groups", New XAttribute("name", gn), g))
newXML =
<root>
<Groups name="first">
<element>
<id>1</id>
<group>first</group>
</element>
<element>
<id>3</id>
<group>first</group>
</element>
</Groups>
<Groups name="second">
<element>
<id>2</id>
<group>second</group>
</element>
</Groups>
</root>
Similar to other answers.

Parse XML into DataTable

I have the following XML:
<RECORDS>
<RECORD>
<PROPERTY NAME="FNAME" TYPE="string"></PROPERTY>
<PROPERTY NAME="LNAME" TYPE="string">
<VALUE>SMITH</VALUE>
</PROPERTY>
<PROPERTY NAME="ZIP" TYPE="string">
<VALUE></VALUE>
</PROPERTY>
<PROPERTY NAME="PHONE" TYPE="string">
<VALUE>5551212</VALUE>
</PROPERTY>
</RECORD>
<RECORD>
....
</RECORD>
<RECORD>
....
</RECORD>
<RECORD>
....
</RECORD>
</RECORDS>
There may be 1000 "RECORD" nodes under "RECORDS". I am trying to simply dump each record into a data row (or the entire thing into a datatable). I'd be happy to just get all the "VALUE"s into each row's column. Is there an efficient way to do this in C#? Doing a foreach() and accessing each property one at a time seems awfully slow.
I imagine it would involve Row.ItemArray or maybe XMLArray or something along those lines.
Interested to see a good solution to this!
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<RECORDS>" +
"<RECORD>" +
"<PROPERTY NAME=\"FNAME\" TYPE=\"string\"></PROPERTY>" +
"<PROPERTY NAME=\"LNAME\" TYPE=\"string\">" +
"<VALUE>SMITH</VALUE>" +
"</PROPERTY>" +
"<PROPERTY NAME=\"ZIP\" TYPE=\"string\">" +
"<VALUE></VALUE>" +
"</PROPERTY>" +
"<PROPERTY NAME=\"PHONE\" TYPE=\"string\">" +
"<VALUE>5551212</VALUE>" +
"</PROPERTY>" +
"</RECORD>" +
"</RECORDS>";
XDocument doc = XDocument.Parse(xml);
DataTable dt = new DataTable();
List<XElement> properties = doc.Descendants("PROPERTY").ToList();
//add columns to table
foreach (XElement property in properties)
{
string name = property.Attribute("NAME").Value;
string _type = property.Attribute("TYPE").Value;
if(!dt.Columns.Contains(name))
{
dt.Columns.Add(name, Type.GetType("System." + _type, false, true));
}
}
//add rows to table
foreach(XElement record in doc.Descendants("RECORDS").ToList())
{
DataRow newRow = dt.Rows.Add();
foreach (XElement property in properties)
{
string name = property.Attribute("NAME").Value;
Type _type = Type.GetType("System." + property.Attribute("TYPE").Value, false, true);
string value = property.Value;
switch (_type.ToString())
{
case "System.String" :
newRow[name] = (string)value;
break;
case "System.Int" :
newRow[name] = int.Parse(value);
break;
}
}
}
}
}
}

Categories