Here's how I started:
System.Xml.Serialization.XmlRootAttribute xRoot = new System.Xml.Serialization.XmlRootAttribute();
xRoot.IsNullable = true;
xRoot.Namespace = "urn:schemas-microsoft-com:rowset";
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ProductSalesList), xRoot);
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(path + "\\" + file);
eltOnly e = (eltOnly)serializer.Deserialize(reader);
But I don't know how to continue. Here's the xml file:
<xml xmlns:s='uuid:00000000-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-0000-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly'>
<s:AttributeType name='Art' rs:number='1' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='string' dt:maxLength='255'/>
</s:AttributeType>
<s:AttributeType name='Name' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='string' dt:maxLength='255'/>
</s:AttributeType>
<s:AttributeType name='Sum' rs:number='3' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='float' dt:maxLength='8' rs:precision='15' rs:fixedlength='true'/>
</s:AttributeType>
<s:AttributeType name='Cost' rs:number='4' rs:nullable='true' rs:maydefer='true' rs:writeunknown='true'>
<s:datatype dt:type='float' dt:maxLength='8' rs:precision='15' rs:fixedlength='true'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<z:row Art='0000000001' Name='Brand pils 0,2' Sum='153' Cost='304'/>
<z:row Art='0000000002' Name='Brand pils 0,25' Sum='11' Cost='25.300000000000004'/>
<z:row Art='0000000003' Name='Brand pils 0,5' Sum='3' Cost='13.799999999999999'/>
</rs:data>
</xml>
How to continue? How should I call the class which would have the Deserialization attribute?
Here's what I did:
[Serializable()]
public class Elt
{
[System.Xml.Serialization.XmlElement]
public string Art { get; set; }
[System.Xml.Serialization.XmlElement]
public string Name { get; set; }
[System.Xml.Serialization.XmlElement]
public float? Sum { get; set; }
[System.Xml.Serialization.XmlElement]
public float? Cost { get; set; }
}
[Serializable, System.Xml.Serialization.XmlRoot("eltOnly")]
public class eltOnly
{
[System.Xml.Serialization.XmlElement]
public List<Elt> Elt { get; set; }
}
And I get error: There is an error in XML document (1, 2). " was not expected."
This linq2xml should do what you want...
XDocument doc=XDocument.Load(yourXml);
XNamespace rs="urn:schemas-microsoft-com:rowset";
XNamespace z="#RowsetSchema";
var lstRows=doc.Descendants(rs+"data").Elements(z+"row").Select(x=>
new
{
art=x.Attribute("Art").Value,
name=x.Attribute("Name").Value,
sum=(float?)x.Attribute("Sum"),
cost=(float?)x.Attribute("Cost")
}
);
You can now iterate over lstRows
foreach(var row in lstRows)
{
row.art;//string
row.name;//string
row.sum;//float?
row.cost;//float?
}
Related
I have this XML structure:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope">
<SOAP-ENV:Header>
<dlv:delivery xmlns:dlv="http://schemas.biztalk.org/btf-2-0/delivery">
<dlv:message>
<dlv:messageID>SDID20200921053955</dlv:messageID>
<dlv:sent>2020-09-21T05:39:55</dlv:sent>
</dlv:message>
<dlv:to>
<dlv:address>urn:schindler:SchindlerDraw:prod</dlv:address>
</dlv:to>
<dlv:from>
<dlv:address>urn:schindler:logical-system:CRM</dlv:address>
</dlv:from>
<dlv:from>
<dlv:system>PC1</dlv:system>
</dlv:from>
</dlv:delivery>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<SALESORDER>
<EXTENSIONIN>
<item>
<CONFIRMATIONPRINTDATE />
<CUSTOMEROFFERNOTE />
<CUSTOMERREFERENCE />
</item>
</EXTENSIONIN>
</SALESORDER>
</asx:values>
</asx:abap>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Does anyone know how I can get all the values from the element "item"?
The result should be:
CONFIRMATIONPRINTDATE
CUSTOMEROFFERNOT
CUSTOMERREFERENCE
Thank you in advance.
Use 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);
XElement delivery = doc.Descendants().Where(x => x.Name.LocalName == "delivery").FirstOrDefault();
XNamespace dlv = delivery.GetNamespaceOfPrefix("dlv");
Envelope envelope = new Envelope();
envelope.messageId = (string)delivery.Descendants(dlv + "messageID").FirstOrDefault();
envelope.sent = (DateTime)delivery.Descendants(dlv + "sent").FirstOrDefault();
envelope.toAddr = (string)delivery.Descendants(dlv + "from").FirstOrDefault().Element(dlv + "address");
envelope.fromAddr = (string)delivery.Descendants(dlv + "to").FirstOrDefault().Element(dlv + "address");
envelope.system = (string)delivery.Descendants(dlv + "system").FirstOrDefault();
envelope.items = doc.Descendants("item").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
public class Envelope
{
public string messageId { get; set; }
public DateTime sent { get; set; }
public string toAddr { get; set; }
public string fromAddr { get; set; }
public string system { get; set; }
public Dictionary<string,string> items { get; set; }
}
}
Many Thanks in advance for any help in creating a class and deserializing xml file like below in .NET
example of xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Article>
<Story>
<Title>Some Title 1</Title>
<Author>John</Author>
<Lead>Some Lead 1</Lead>
<Subtitle>SubTitle 1-1</Subtitle>
<Body>body 1-1-1</Body>
<Body>body 1-1-2</Body>
<Body>body 1-1-3</Body>
<Body>body 1-1-4</Body>
<Subtitle>SubTitle 1-2</Subtitle>
<Body>body 1-2-1</Body>
<Body>body 1-2-2</Body>
<Subtitle>SubTitle 1-3</Subtitle>
<Body>body 1-3-1</Body>
<Body>body 1-3-2</Body>
<Body>body 1-3-3</Body>
</Story>
<Story>
<Title>Some Title 2</Title>
<Author>Adam</Author>
<Lead>Some Lead 2</Lead>
<Subtitle>SubTitle 2-1</Subtitle>
<Body>body 2-1-1</Body>
<Body>body 2-1-2</Body>
<Body>body 2-1-3</Body>
<Subtitle>SubTitle 2-2</Subtitle>
<Body>body 2-2-1</Body>
<Body>body 2-2-2</Body>
<Subtitle>SubTitle 2-3</Subtitle>
<Body>body 2-3-1</Body>
</Story>
<Story>
<Picture>
<Image href="someFile1.jpg"></Image>
<Credit>Credit 1</Credit>
<Description>Description Image 1</Description>
</Picture>
</Story>
<Story>
<Picture>
<Image href="someFile2.jpg"></Image>
<Credit>Credit 2</Credit>
</Picture>
</Story>
</Article>
</Root>
I have prepared Domain class for like this (but maybe it's not the best idea for my xml file)
using System.Collections.Generic;
using System.Xml.Serialization;
namespace QgeImagingXmlConnector.Domain
{
[XmlRoot(ElementName = "Root")]
public class InputXmlModel
{
[XmlElement("Article")]
public List<Article> Articles { get; set; }
}
public class Article
{
[XmlElement("Story")]
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
public List<Item> Items { get; set; }
//OR
public List<StoryPicture> Pictures { get; set; }
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
and method for deserialize
public InputXmlModel GetInputXmlModelByXmlFile(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(InputXmlModel));
TextReader tr = new StreamReader(filePath);
var result = (InputXmlModel)serializer.Deserialize(tr);
tr.Close();
return result;
}
And my question is: How to change my class to work ( by adding some attributes or change structure )
STORY could by like Story with some content or Story with only picture
so in my class i added 2 classes Story and StoryPicture
In Content of Story we can have many body or subtitle tags - but the order is important
Regards
P.S.
this is what i want to get as an result
I put data only for first Story
var result = new InputXmlModel
{
Articles = new List<Article>
{
{
new Article
{
Stories = new List<Story>
{
{new Story
{
Title = "Some Title 1",
Author = "John",
Lead="Some Lead 1",
Items = new List<Item>
{
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-1"},
new Item{ItemType = "Body", ItemText = "body 1-1-1"},
new Item{ItemType = "Body", ItemText = "body 1-1-2"},
new Item{ItemType = "Body", ItemText = "body 1-1-3"},
new Item{ItemType = "Body", ItemText = "body 1-1-4"},
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-2"},
new Item{ItemType = "Body", ItemText = "body 1-2-1"},
new Item{ItemType = "Body", ItemText = "body 1-2-2"},
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-3"},
new Item{ItemType = "Body", ItemText = "body 1-3-1"},
new Item{ItemType = "Body", ItemText = "body 1-3-2"},
new Item{ItemType = "Body", ItemText = "body 1-3-3"},
}
}
}
// here next 3 stories ( one with Items, two for pictures )
}
}
}
}
};
Yes you can do this too like this.
Your model class would like:
using System.Collections.Generic;
using System.Xml.Serialization;
namespace QgeImagingXmlConnector.Domain
{
[XmlRoot(ElementName = "Root")]
public class InputXmlModel
{
[XmlElement("Article")]
public List<Article> Articles { get; set; }
}
public class Article
{
[XmlElement("Story")]
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
[XmlElement("Item")]
public List<Item> Items { get; set; }
[XmlElement("Picture")]
public List<StoryPicture> Pictures { get; set; }
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
And your XML like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Article>
<Story>
<Title>Some Title 1</Title>
<Author>John</Author>
<Lead>Some Lead 1</Lead>
<Subtitle>SubTitle 1-1</Subtitle>
<Body>body 1-1-1</Body>
<Body>body 1-1-2</Body>
<Body>body 1-1-3</Body>
<Body>body 1-1-4</Body>
<Subtitle>SubTitle 1-2</Subtitle>
<Body>body 1-2-1</Body>
<Body>body 1-2-2</Body>
<Subtitle>SubTitle 1-3</Subtitle>
<Body>body 1-3-1</Body>
<Body>body 1-3-2</Body>
<Body>body 1-3-3</Body>
</Story>
<Story>
<Title>Some Title 2</Title>
<Author>Adam</Author>
<Lead>Some Lead 2</Lead>
<Subtitle>SubTitle 2-1</Subtitle>
<Body>body 2-1-1</Body>
<Body>body 2-1-2</Body>
<Body>body 2-1-3</Body>
<Subtitle>SubTitle 2-2</Subtitle>
<Body>body 2-2-1</Body>
<Body>body 2-2-2</Body>
<Subtitle>SubTitle 2-3</Subtitle>
<Body>body 2-3-1</Body>
<Picture>
<Image href="someFile1.jpg"></Image>
<Credit>Credit 1</Credit>
<Description>Description Image 1</Description>
</Picture>
<Picture>
<Image href="someFile2.jpg"></Image>
<Credit>Credit 2</Credit>
</Picture>
</Story>
</Article>
</Root>
Is that what you're looking for?
Try following xml linq :
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication100
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Article article = doc.Descendants("Article").Select(x => new Article() { Stories = x.Elements("Story").Select(y => Story.ParseStory(y)).ToList() }).FirstOrDefault();
}
}
public class InputXmlModel
{
public List<Article> Articles { get; set; }
}
public class Article
{
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
public List<Item> Items { get; set; }
public List<StoryPicture> Pictures { get; set; }
enum State
{
DEFAULT,
SUBTITLE,
}
public static Story ParseStory(XElement xStory)
{
Story story = new Story();
State state = State.DEFAULT;
Item newItem = null;
StoryPicture newPicture = null;
foreach (XElement child in xStory.Elements())
{
switch(state)
{
case State.DEFAULT :
switch (child.Name.LocalName)
{
case "Title" :
story.Title = (string)child;
break;
case "Author":
story.Author = (string)child;
break;
case "Lead":
story.Lead = (string)child;
break;
case "Subtitle":
newItem = new Item();
if (story.Items == null) story.Items = new List<Item>();
story.Items.Add(newItem);
state = State.SUBTITLE;
break;
case "Picture":
newPicture = new StoryPicture()
{
ImageHref = (string)child.Element("Image").Attribute("href"),
Credit = (string)child.Element("Credit"),
Description = (string)child.Element("Description")
};
if (story.Pictures == null) story.Pictures = new List<StoryPicture>();
story.Pictures.Add(newPicture);
break;
default:
Console.WriteLine("Error");
Console.ReadLine();
break;
}
break;
case State.SUBTITLE :
switch (child.Name.LocalName)
{
case "Body" :
newItem.ItemType = "SubTitle";
newItem.ItemText = (string)child;
break;
case "Subtitle":
newItem = new Item();
if (story.Items == null) story.Items = new List<Item>();
story.Items.Add(newItem);
break;
default:
Console.WriteLine("Error");
Console.ReadLine();
break;
}
break;
}
}
return story;
}
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
If you take out the <Root> tag in your XML and change your method for the deserialization to:
public InputXmlModel GetInputXmlModelByXmlFile(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(Article));
using (FileStream fileStream = new FileStream("<PathToFile>", FileMode.Open))
{
Article result = (Article)serializer.Deserialize(fileStream);
}
}
It should work.
Sample XML:
<query yahoo:count="1" yahoo:created="2016-03-31T06:43:49Z" yahoo:lang="en-US">
<results>
<channel>
<item>
<yweather:condition code="28" date="Thu, 31 Mar 2016 08:00 AM SAST" temp="58" text="Mostly Cloudy"/>
</item>
</channel>
</results>
</query>
Code:
string weburl = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22Cape%20Town%22%29&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var xml = await new WebClient().DownloadStringTaskAsync(new Uri(weburl));
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//query/results/channel/item");
foreach (XmlNode node in nodes)
{
MessageBox.Show(node.InnerXml);
}
I have been struggling to just get the temp and text outputed but I can't find way how to, this is as far as I got.
You can access XML attributes from XmlNode.Attributes property :
var condition = doc.SelectSingleNode("/query/results/channel/item/*");
MessageBox.Show(condition.Attributes["text"].Value);
MessageBox.Show(condition.Attributes["temp"].Value);
Try this....
Usings....
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
Classes....
[XmlRoot(ElementName = "condition", Namespace = "http://xml.weather.yahoo.com/ns/rss/1.0")]
public class Condition
{
[XmlAttribute(AttributeName = "yweather", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Yweather { get; set; }
[XmlAttribute(AttributeName = "code")]
public string Code { get; set; }
[XmlAttribute(AttributeName = "date")]
public string Date { get; set; }
[XmlAttribute(AttributeName = "temp")]
public string Temp { get; set; }
[XmlAttribute(AttributeName = "text")]
public string Text { get; set; }
}
[XmlRoot(ElementName = "item")]
public class Item
{
[XmlElement(ElementName = "condition", Namespace = "http://xml.weather.yahoo.com/ns/rss/1.0")]
public Condition Condition { get; set; }
}
[XmlRoot(ElementName = "channel")]
public class Channel
{
[XmlElement(ElementName = "item")]
public Item Item { get; set; }
}
[XmlRoot(ElementName = "results")]
public class Results
{
[XmlElement(ElementName = "channel")]
public Channel Channel { get; set; }
}
[XmlRoot(ElementName = "query")]
public class Query
{
[XmlElement(ElementName = "results")]
public Results Results { get; set; }
[XmlAttribute(AttributeName = "yahoo", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Yahoo { get; set; }
[XmlAttribute(AttributeName = "count", Namespace = "http://www.yahooapis.com/v1/base.rng")]
public string Count { get; set; }
[XmlAttribute(AttributeName = "created", Namespace = "http://www.yahooapis.com/v1/base.rng")]
public string Created { get; set; }
[XmlAttribute(AttributeName = "lang", Namespace = "http://www.yahooapis.com/v1/base.rng")]
public string Lang { get; set; }
}
Code...
XmlDocument xmlDocument = new XmlDocument();
try
{
xmlDocument.Load("https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22Cape%20Town%22%29&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
string XMLxmlDocument = xmlDocument.InnerXml.ToString();
byte[] BUFXML = ASCIIEncoding.UTF8.GetBytes(XMLxmlDocument);
MemoryStream ms1 = new MemoryStream(BUFXML);
XmlSerializer DeserializerPlaces = new XmlSerializer(typeof(Query));//, new XmlRootAttribute("Query"));
using (XmlReader reader = new XmlTextReader(ms1))
{
Query dezerializedXML = (Query)DeserializerPlaces.Deserialize(reader);
string temp = dezerializedXML.Results.Channel.Item.Condition.Temp;
string text = dezerializedXML.Results.Channel.Item.Condition.Text;
}// Put a break-point here, then mouse-over temp and text, you should have you values (dezerializedXML contains the entire object)
}
catch (System.Exception)
{
throw;
}
I used xml linq along with Regex. I had to fix issues with your xml. I think the main issue was the namespaces.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Root xmlns:yahoo=\"abc\" xmlns:yweather=\"def\">" +
"<query yahoo:count=\"1\" yahoo:created=\"2016-03-31T06:43:49Z\">" +
"yahoo:lang=\"en-US\"><results>" +
"<channel>" +
"<item>" +
"<yweather:condition>" +
"code=\"28\" date=\"Thu, 31 Mar 2016 08:00 AM SAST\" temp=\"58\" text=\"Mostly Cloudy\"/>" +
"</yweather:condition>" +
"</item>" +
"</channel>" +
"</results>" +
"</query>" +
"</Root>";
XDocument doc = XDocument.Parse(xml);
string innertext = doc.Descendants().Where(x => x.Name.LocalName == "condition").Select(y => y.Value).FirstOrDefault();
string pattern = "\\s?(?'name'[^=]+)=\"(?'value'[^\"]+)\"";
MatchCollection matches = Regex.Matches(innertext, pattern);
foreach (Match match in matches)
{
Console.WriteLine("Name : {0}, Value : {1}", match.Groups["name"].Value, match.Groups["value"].Value);
}
Console.ReadLine();
}
}
}
I have following xml :
in other standard xml like this question here we have no problem but in php web service :
http://sandoghche.com/WebService
for inbox check my inbox xml is this:
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<Result>
<text_message>
<id>509</id>
<message><![CDATA[a]]></message>
<time>1323519941</time>
<message_phone>
<cellphone>09196070718</cellphone>
</message_phone>
</text_message>
<text_message>
<id>507</id>
<message>1</message>
<time>1323519803</time>
<message_phone>
<cellphone>09360437392</cellphone>
</message_phone>
</text_message>
<text_message>
<id>303</id>
<message><![CDATA[smsm text]]></message>
<time>1318343296</time>
<message_phone>
<cellphone>09354338365</cellphone>
</message_phone>
</text_message>
<text_message>
<id>219</id>
<message><![CDATA[my sms texts here]]></message>
<time>1316154042</time>
<message_phone>
<cellphone>09127930265</cellphone>
</message_phone>
</text_message>
<text_message>
<id>217</id>
<message>2</message>
<time>1316090189</time>
<message_phone>
<cellphone>09195533234</cellphone>
</message_phone>
</text_message>
</Result>
for this I wrote the following code
public class SMSData
{
public int id { set; get; }
public string message { set; get; }
public string cellphone { set; get; }
}
string data = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Result><text_message><id>509</id><message><![CDATA[a]]></message><time>1323519941</time><message_phone><cellphone>09196070718</cellphone></message_phone></text_message><text_message><id>507</id><message>1</message><time>1323519803</time><message_phone><cellphone>09360437392</cellphone></message_phone></text_message><text_message><id>303</id><message><![CDATA[ممنون.شما؟]]></message><time>1318343296</time><message_phone><cellphone>09354378365</cellphone></message_phone></text_message><text_message><id>219</id><message><![CDATA[سلام اقاي طباطبايي لينك مربوط به ورود اعضا به وبلاگ روي صفحه فيلتر مي رود با احترام احدي]]></message><time>1316154042</time><message_phone><cellphone>09127960265</cellphone></message_phone></text_message><text_message><id>217</id><message>2</message><time>1316090189</time><message_phone><cellphone>09195523234</cellphone></message_phone></text_message></Result>";
Stream s = GenerateStreamFromString(data);
XmlReader xr = XmlReader.Create(s);
while (xr.Read())
{
if ((xr.NodeType == XmlNodeType.Element) && (xr.Name == "text_message"))
{
if (xr.HasValue)
{
string id = xr.GetAttribute("id");
string message = xr.GetAttribute("message");
}
}
}
and this code
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
XmlNodeList xnList = xml.SelectNodes("/Result");
foreach (XmlNode xn in xnList)
{
but can't catch values on this xml document.
Is there a clean way to do this?
It is easier to use the built-in Serializers for these kind of things, like the XmlSerializer or the DataContractSerializer. I used the latter and tested your XML with the following snippet.
First annotate your SMSData class and create a new class to hold the Message_phone elements in a new class:
[DataContract(Name="text_message",Namespace = "")]
public class SMSData
{
[DataMember(Order =1)]
public int id { set; get; }
[DataMember(Order = 2)]
public string message { set; get; }
[DataMember(Order = 3)]
public int time { set; get; }
[DataMember(Order = 4)]
public Message_phone message_phone { set; get; }
// for easy access to cellphone
public string cellphone {
get
{
return message_phone!=null?message_phone.cellphone:null;
}
}
}
[DataContract(Name="message_phone",Namespace = "")]
public class Message_phone
{
[DataMember]
public string cellphone { set; get; }
}
and then create an instance of the DataContractSerializer for your array of SMSData, and call ReadObject on it like so:
Stream s = GenerateStreamFromString(data);
var ds = new DataContractSerializer(
typeof(SMSData[]), // array of SMSdata
"Result", // top level element
"", // empty namespace
new List<Type> { typeof(SMSData[])} );
var smsdata = (SMSData[]) ds.ReadObject(XmlReader.Create(s));
// smsdata now holds the SMSData from the Xml file
I have XML as below which I am loading into XDocument
<ConversionSpecification xmlns="http://tempuri.org/X12ParserSpecification.xsd" TransactionSetIdentifierCode="837">
<Loop LoopId="1000A" Name="SNAME" Identifier="NM1*41">
<Segment SegmentId="NM1" Usage="R"/>
<Segment SegmentId="PER" Usage="R"/>
</Loop>
<Loop LoopId="1000B" Name="RNAME" Identifier="NM1*40">
<Segment SegmentId="NM1" Usage="R"/>
</Loop>
<Loop LoopId="2000B" Name="SHLOOP" Identifier="22">
<Segment SegmentId="SBR" Usage="R"/>
<Segment SegmentId="PAT" Usage="S"/>
</Loop>
</ConversionSpecification>
XDocument document = XDocument.Load("Files/Loops.xml");
XNamespace ns = XNamespace.Get("http://tempuri.org/X12ParserSpecification.xsd");
var loopInfo = from loop in document.Descendants(ns + "Loop")
select new Loop
{
LoopID = loop.Attribute("LoopId").Value,
LoopIdentifier = loop.Attribute("Identifier").Value
LoopSegments =
LoopUsage =
};
Where Loop is:
class Loop
{
public string LoopID { get; set; }
public string LoopIdentifier { get; set; }
public string[] LoopSegments { get; set; }
public string[] LoopUsage { get; set; }
}
Now I would like to assign LoopElements with SegmentID attribute values and LoopUsage with usage attribute values. Is there a way to do it with a single subquery?
Is this what you're after?
var loopInfo = from loop in document.Descendants(ns + "Loop")
select new Loop
{
LoopID = loop.Attribute("LoopId").Value,
LoopIdentifier = loop.Attribute("Identifier").Value,
LoopSegments = loop.Descendants(ns + "Segment").Select(s => s.Attribute("SegmentId").Value).ToArray(),
LoopUsage = loop.Descendants(ns + "Segment").Select(s => s.Attribute("Usage").Value).ToArray()
};
The output looks like this: