<query xmlns="urn:xmpp:dialogueHistory">
<dialogueHistory id="4d38f289-9">
<MessageID>26164</MessageID>
<messageText>Fhh</messageText>
<msgDate>11/25/2016 6:30:39 AM</msgDate>
<unReadCount>0</unReadCount>
</dialogueHistory>
</query>
get information of messageid,messagetext
how can I get the information from the above xml using agsxmpp library
namespace is agsXMPP.Xml.Dom
You need to specify the namespace. Using 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);
List<XElement> dialogueHistory = doc.Descendants().Where(x => x.Name.LocalName == "dialogueHistory").ToList();
XNamespace ns = dialogueHistory.FirstOrDefault().GetDefaultNamespace();
var results = dialogueHistory.Select(x => new {
id = (string)x.Attribute("id"),
messageId = (int)x.Element(ns + "MessageID"),
messageText = (string)x.Element(ns + "messageText"),
messageDate = (DateTime)x.Element(ns + "msgDate"),
unReadCount = (int)x.Element(ns + "unReadCount")
}).ToList();
}
}
}
Related
I'm really struggling to find a way to pass an XML string without escaping the characters in C#. I have a string below that needs to be passed into a call to a WSDL as a string.
<MyElement ElementId='xxxxxx'><Segments><Segment Status='1' SegmentId='xx1'/><Segment Status='1'
SegmentId='xx2'/></Segments></MyElement>
If I try to pass it has a string the characters are escaping characters such as '>'. I imported the WSDL in a .NET Console application. Here is what that string above is going into as I see using the Wizdler tool in chrome.
The entire message is this.
So my question is how in C# can I use the WSDL or even just do it manually to make the call to make this work
using (var client = new MemberService.MemberWebServiceSoapClient())
{
var xml = "<MyElement ElementId='xxxxxxx'><Segments><Segment Status='1' SegmentId='xx1'/><Segment
Status='1' SegmentId='xx2'/></Segments></MyElement>"
var send = client.Moving(xml);
}
Use 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 envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<Body>" +
"<Moving xmlns=\"http://www.retalix.com/HQLWebServices\">" +
"</Moving>" +
"</Body>" +
"</Envelope>";
XDocument doc = XDocument.Parse(envelope);
XElement moving = doc.Descendants().Where(x => x.Name.LocalName == "Moving").FirstOrDefault();
XNamespace ns = moving.GetDefaultNamespace();
string elementId = "xxxxxx";
List<Segment> segments = new List<Segment>() {
new Segment() { id = "xx1", status = 1},
new Segment() { id = "xx2", status = 1}
};
XElement xMyElement = new XElement(ns + "MyElement", new XAttribute("ElementId", elementId));
moving.Add(xMyElement);
XElement xSegments = new XElement(ns + "Segments");
xMyElement.Add(xSegments);
foreach (Segment segment in segments)
{
XElement xSegment = new XElement("Segment", new object[] {
new XAttribute("Status", segment.status),
new XAttribute("SegmentId", segment.id)
});
xSegments.Add(xSegment);
}
string sendXml = doc.ToString();
}
}
public class Segment
{
public string id { get; set; }
public int status { get; set; }
}
}
I have a soap response, I want to remove all soap related things and get the inner XML with only one header.
response:
"<s:Envelope xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body>
<getResponse xmlns = ""http://tempuri.org/"">
<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
<Id>999</Id>
<EResponse>
<au>
<st>ABC</st>
<co>DGH</co>
<mo>MMM</mo>
</au>
</EResponse>
</getResult>
</getResponse>
</s:Body>
</s:Envelope>
I want to extract this:
<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
<Id>999</Id>
<EResponse>
<au>
<st>ABC</st>
<co>DGH</co>
<mo>MMM</mo>
</au>
</EResponse>
</getResult>
I am trying this:
XDocument input;
using (var nodeReader = new XmlNodeReader(doc))
{
nodeReader.MoveToContent();
input = XDocument.Load(nodeReader);
}
var xns = XNamespace.Get("http://tempuri.org/");
var SoapBody = input.Descendants(xns + "getResult").First().ToString();
but I am also getting namespace tempuri appended in getresult tag, which I don't want or any other way to achieve it ?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication169
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XElement results = doc.Descendants().Where(x=> x.Name.LocalName == "getResult").FirstOrDefault();
XNamespace ns = results.GetDefaultNamespace();
string id = (string)results.Element(ns + "Id");
Dictionary<string, string> au = results.Descendants(ns + "au").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
I am looking for a way to convert an xml stream to csv, but I only find solution for 1 collection, i.e. my xml looks like :
<?xml version="1.0" encoding="UTF-8"?>
<CompactData>
<Header>
<ID>id_ofç=_file</ID>
<Test>false</Test>
</Header>
<data:DataSet>
<data:Series FREQ="M" item="item1" unit="unit1">
<data:Obs TIME_PERIOD="2015-01" OBS_VALUE="5.47" />
<data:Obs TIME_PERIOD="2015-02" OBS_VALUE="5.01" />
<data:Obs TIME_PERIOD="2015-03" OBS_VALUE="5.39" />
</data:Series>
<data:Series FREQ="M" item="item2" unit="unit2">
<data:Obs TIME_PERIOD="2015-01" OBS_VALUE="5.47" />
<data:Obs TIME_PERIOD="2015-02" OBS_VALUE="5.01" />
<data:Obs TIME_PERIOD="2015-03" OBS_VALUE="5.39" />
</data:Series>
</data:DataSet>
</CompactData>
Here I want a csv with the format :
FREQ,item,unit,TIME_PERIOD,OBS_VALUE
what is the best way to do that?
Thanks!
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication166
{
class Program
{
const string XML_FILENAME = #"c:\temp\test.xml";
const string CSV_FILENAME = #"c:\temp\test.csv";
static void Main(string[] args)
{
StreamWriter writer = new StreamWriter(CSV_FILENAME);
writer.WriteLine(string.Join(",", new string[] {"FREQ","item","unit","TIME_PERIOD","OBS_VALUE"}));
XDocument doc = XDocument.Load(XML_FILENAME);
XElement dataSet = doc.Descendants().Where(x => x.Name.LocalName == "DataSet").FirstOrDefault();
XNamespace nsData = dataSet.GetNamespaceOfPrefix("data");
foreach (XElement series in dataSet.Elements(nsData + "Series"))
{
string freq = (string)series.Attribute("FREQ");
string item = (string)series.Attribute("item");
string unit = (string)series.Attribute("unit");
foreach (XElement obs in series.Elements(nsData + "Obs"))
{
DateTime time = DateTime.ParseExact((string)obs.Attribute("TIME_PERIOD"), "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture);
double value = (double)obs.Attribute("OBS_VALUE");
writer.WriteLine(string.Join(",", new string[] {freq, item, unit,time.ToString(), value.ToString()}));
}
}
writer.Flush();
writer.Close();
}
}
}
I having XML as follow and I need to read this XML and get specific element in the XML and save into excel.
Below is my XML File :
<?xml version="1.0" encoding="UTF-8"?>
<ns1:BookTestXMLExport StartTime="2016-09-20T12:58:15.000+07:00" scanTime="2016-09-20T12:58:15.000+07:00" scanStatus="Pass">
<ns1:MachineXML barCode="ISN-1000500213" Revision="2016A" bookType="Novel"/>
<ns1:StationXML scannerName="HP4512745" stage="v810"/>
<ns1:ScanXML name="32:2:165:1">
<ns1:IndictmentXML algorithm="NIL" subType="17X8X15MM" imageFileName="175228000_9_0.jpg">
<ns1:BorrowXML packageId="NIL" userId="NIL" name="NIL"/>
<ns1:BookXML name="GrayDay" desc="Love Story"/>
</ns1:IndictmentXML>
</ns1:ScanXML>
<ns1:ScanXML name="35:23:165:1">
<ns1:IndictmentXML algorithm="NIL" subType="17X8X15MM" imageFileName="175228001_9_0.jpg">
<ns1:BorrowXML packageId="NIL" userId="8799" name="Sharon"/>
<ns1:BookXML name="Harry Potter" desc="Magic"/>
</ns1:IndictmentXML>
</ns1:ScanXML>
</ns1:BookTestXMLExport>
Below is the expected result:
Expected Result
Can anyone guide me on this.
Try this to parse file. You still need to save as excel.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication14
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement bookTestXMLExport = doc.Descendants().Where(x => x.Name.LocalName == "BookTestXMLExport").FirstOrDefault();
XNamespace ns = bookTestXMLExport.GetNamespaceOfPrefix("ns1");
var results = doc.Descendants(ns + "BookTestXMLExport").Select(x => new
{
scanStatus = (string)x.Attribute("scanStatus"),
barCode = (string)x.Element(ns + "MachineXML").Attribute("barCode"),
ScanXML = x.Elements(ns + "ScanXML").Select( y => new {
name = (string)y.Descendants(ns + "BookXML").FirstOrDefault().Attribute("name"),
desc = (string)y.Descendants(ns + "BookXML").FirstOrDefault().Attribute("desc")
}).ToList()
}).ToList();
}
}
}
I have this XML file : http://dl.dropbox.com/u/10773282/2011/perf.xml
It has two Class elements as is marked. I need to get two nodes with C#.
With Python, I can easily get them with etree.ElementTree as follows.
from xml.etree import ElementTree as et
from xml.etree.ElementTree import Element
tree = et.parse("perf.xml")
tss = tree.getiterator('Class')
for elem in tss:
tss_name = elem.find('ClassKeyName')
print tss_name.text
>> helloclass.exe
>> helloclass.exeFpga::TestMe
How can I do the same thing with C#?
SOLVED
using System;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
namespace HIR {
class Dummy {
static void Main(String[] argv) {
XDocument doc = XDocument.Load("perf.xml");
var res = from p in doc.Root.Elements("Module").Elements("NamespaceTable").Elements("Class").Elements("ClassKeyName") select p.Value;
foreach (var val in res) {
Console.WriteLine(val.ToString());
}
}
}
}
>> helloclass.exe
>> helloclass.exeFpga::TestMe
Or
foreach (var elem in elems) {
var res = elem.Elements("ClassKeyName").ToList();
Console.WriteLine(res[0].Value);
}
You should try Linq to XML... Quite easy to use:
var xml = XDocument.Load(filename);
var res = from p in xml.Root.Elements("Class").Elements("ClassKeyName") select p.Value;
Try:
using System.Xml;
// ...
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filename);
var matches = xmlDoc.SelectNodes("//Class/ClassKeyName");