简体   繁体   中英

Need help reading using LINQ to XML reading XML File

I have the following XML File:

<DATASET>
<MESSAGE TYPE="TYPE1 ">
<FROM>
</FROM>
<EMAILRECIPIENTS>
<TO>
email@email.com
</TO>
</EMAILRECIPIENTS>
<SUBJECT>
tHIS IS A SUBJECT
</SUBJECT>
<BODY>
THIS is the bidoy
</BODY>
<ATTACHED>
<CONTENT>                this is some content                </CONTENT>
<CONTENT>                this is some content                </CONTENT>
<CONTENT>                this is some content                </CONTENT>
<CONTENT>                this is some content                </CONTENT>
<CONTENT>                this is some content                </CONTENT>
</ATTACHED>
</MESSAGE>
<MESSAGE TYPE="TYPE1 ">
<FROM>
</FROM>
<EMAILRECIPIENTS>
<TO>
email2@email.com
</TO>
</EMAILRECIPIENTS>
<SUBJECT>
this IS A SUBJECT
</SUBJECT>
<BODY>
THIS is the body
</BODY>
<ATTACHED>
<CONTENT>                this is some content2                </CONTENT>
<CONTENT>                this is some content2                </CONTENT>
<CONTENT>                this is some content2                </CONTENT>
<CONTENT>                this is some content2                </CONTENT>
<CONTENT>                this is some content2               </CONTENT>
</ATTACHED>
</MESSAGE>
<MESSAGE TYPE="TYPE1 ">
<FROM>
</FROM>
<EMAILRECIPIENTS>
<TO>
email3@email.com
</TO>
</EMAILRECIPIENTS>
<SUBJECT>
tHIS IS A SUBJECT
</SUBJECT>
<BODY>
THIS is the body3
</BODY>
<ATTACHED>
<CONTENT>                this is some content3                </CONTENT>
<CONTENT>                this is some content3                </CONTENT>
<CONTENT>                this is some content3                </CONTENT>
<CONTENT>                this is some content3                </CONTENT>
<CONTENT>                this is some content3                </CONTENT>
</ATTACHED>
</MESSAGE>
</DATASET>

Now I have tried to write a class to store this information:

public class Dataset
{
public List<Message> Messages = new List<Message>();
}

public class Message
{
public string MessageType { get; set;}
public string From { get; set; }
public string Recipients { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Attachmentfilename { get; set; }
public string Record { get; set; }
}

Now in my main I tried this:

 List<Message> messagelist = new List<Message(
 (
 from e in XDocument.Load(@"c:\XML\1.XML").Descendants("DATASET")
 select new Message
 {
   MessageType = e.Element("MESSAGE").Attribute("TYPE").Value,
   From = e.Element("MESSAGE").Element("FROM").Value,
   Subject = e.Element("MESSAGE").Element("SUBJECT").Value,
   Body = e.Element("MESSAGE").Element("BODY").Value,
   AttachmentFileName = e.Element("MESSAGE").Element("ATTACHEMENT").Attribute("FILENAME").Value,
   Record = e.Element("MESSAGE").Element("ATTACHEMENT").ELement("CONTENT").Value
 }).ToList());

Now I trimed the values I was getting top just make my point. Am I going about doing this all wrong. I just want to parse the XML file to a list or something that I can then go through and send emails based on the values.

My idea is to take the XML file which could contain one record, or many records. My code isn't working and it is mostly due to me not really knowing what I am doing, any advice on the way to process this, or to use LINQ to XML to parse this file noted above would be great.

I have this running, but it only seems to pull the first message into the list and only the first line of CONTENT.

I am looing to pull the whole XML file to the list, the XML file can have multiple Messages. could be one, could have 20.

I would like the list to accept more than one message, this code only seems to pull the first one.

I would like the CONTENT output to one document where each is a line item which preserves whitespace and each line.

What can I change to make this accept more than one line of the CONTENT and more than one message?

As suggested in my comment I'd rather have the XmlSerializer do the job:

Your Message class

[XmlType("MESSAGE")]
public class Message
{
    [XmlElement("FROM")]
    public string From { get; set; }

    [XmlArray("EMAILRECIPIENTS")]
    [XmlArrayItem("TO")]
    public List<string> Recipients { get; set; }

    [XmlElement("SUBJECT")]
    public string Subject { get; set; }

    [XmlElement("BODY")]
    public string Body { get; set; }

    [XmlArray("ATTACHED")]
    [XmlArrayItem("CONTENT")]
    public List<string> Attachments { get; set; }
}

... and using XmlSerializer is as simple as

var msgs = new XmlSerializer(typeof(List<Message>), new XmlRootAttribute("DATASET"));

using (var sr = new StreamReader(@"c:\XML\1.XML"))
{
    var messages = msgs.Deserialize(sr);
}

First of all, there are no closing </MESSAGE> . Second, your <MESSAGE> does not have <RECORD> . Check your XML again. There is no obvious problem in your code.

the xml document has a tag called EMAILRECIPIENTS but the Dataset class has no matching property - it only has Recipients

Also in your code you have the following line

Record = e.ELement("CONTENT").Value

but in the XML file, all the Content tags are children of an Attached tag

First off, let's clean up your code. You have too many unnecessary things, and are referencing the wrong values. There are no FROM, SUBJECT, CONTENT in the element DATASET. Some of these are children of MESSAGE, and CONTENT is a child of ATTACHED,

List<Message> messagelist = XElement.Load(file)
    .Descendants("MESSAGE")
    .Select(e => new Message
    {
       From = e.Element("FROM").Value,
       Subject = e.Element("SUBJECT").Value,
       Record = string.Join("\n", e.Descendants("CONTENT")
                                   .Select(c => c.Value)
                                   .ToArray())
    }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM