简体   繁体   English

需要帮助,需要使用LINQ to XML阅读XML文件

[英]Need help reading using LINQ to XML reading XML File

I have the following XML File: 我有以下XML文件:

<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. 我只想将XML文件解析为列表或其他内容,然后根据这些值发送电子邮件。

My idea is to take the XML file which could contain one record, or many records. 我的想法是获取可能包含一个或多个记录的XML文件。 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. 我的代码无法正常工作,这主要是由于我并不真正知道自己在做什么,有关处理此问题的任何建议,或者使用LINQ to XML解析上述文件的方法都很棒。

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. 我想将整个XML文件拉到列表中,该XML文件可以包含多个消息。 could be one, could have 20. 可能是一个,可能有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. 我希望将CONTENT输出到一个文档,其中每个文档都是一个保留空格和每一行的行项目。

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: 如我的评论所建议,我宁愿让XmlSerializer来完成这项工作:

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 ...并且使用XmlSerializer很简单

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> . 首先,没有结束</MESSAGE> Second, your <MESSAGE> does not have <RECORD> . 其次,您的<MESSAGE>没有<RECORD> Check your XML again. 再次检查您的XML。 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 xml文档具有一个名为EMAILRECIPIENTS的标签,但Dataset类没有匹配的属性-它仅具有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 但在XML文件中,所有Content标签都是Attached标签的子元素

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. 元素DATASET中没有FROM,SUBJECT,CONTENT。 Some of these are children of MESSAGE, and CONTENT is a child of ATTACHED, 其中一些是MESSAGE的子级,而CONTENT是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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