Ok, I have a small issue reading one of my company's notorious malformed xml files.
Trying to get 5 values from it and save them individually as variables.
Here is an example of the tricky XML.
(I may not be using the right terms, but I couldn't find anything on reading values of this type)
<ONE>
<TWO>
<THREE>
</THREE>
</TWO>
<DATA internalid="1" externalid="2" lastname="lname" firstname="name" date="20.03.2003"/>
</ONE>
So, the data I need is internalid, externalid, lastname, firstname, and date.
What I've been working with so far, and unable to make anything happen.
string xml = (@"C:\1.xml");
var xmlElement = XElement.Load(xml);
var xmlList = (from message in xmlElement.Elements("DATA")
select new
{
internalid = message.Attribute("internalid").Value,
externalid = message.Attribute("externalid").Value,
lastname = message.Attribute("lastname").Value,
firstname = message.Attribute("firstname").Value,
date = message.Attribute("date").Value
}).ToString();
And I'm unable to get it to fly. Not that I'm getting any errors, but when I out this string to a richtextbox or just textbox I get this....
System.Linq.Enumerable+WhereSelectEnumerableIterator
2[System.Xml.Linq.XElement,<>f__AnonymousType0
5[System.String,System.String,System.String,System.String,System.String]]
Also, so I can better research the problem, what is it called when data is INSIDE the tag like that?
Thanks guys!
As @Jon Skeet mentioned you are calling ToString() on a sequence. The following code may get your closer to your desired solution.
var xmlList = (from message in xmlElement.Elements("DATA")
select new
{
internalid = message.Attribute("internalid").Value,
externalid = message.Attribute("externalid").Value,
lastname = message.Attribute("lastname").Value,
firstname = message.Attribute("firstname").Value,
date = message.Attribute("date").Value
});
StringBuilder builder = new StringBuilder();
foreach (var item in xmlList)
{
builder.Append(item);
}
string test = builder.ToString();
As for your question regarding "data is INSIDE the tag like that". These are examples of XML Attributes.
Here's a good resource to start learning linq Introduction to LINQ Queries (C#).