如何从包含XML数据的Outlook .msg文件读取?

时间:2019-02-21 14:17:09

标签: c# xml outlook

因此,我的公用文件夹返回了对我有用的特定公用文件夹中的项目数。这些文件另存为.msg,其中包含纯xml数据。

我希望能够从outlook.msg读取xml数据并将其存储在临时表或内存中,以便可以插入sql server?

这是我到目前为止所拥有的:

 olApp = new Outlook.Application();
            olNs = (Outlook._NameSpace) olApp.GetNamespace("MAPI");
            oFolder = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            oExp = oFolder.GetExplorer(false);
            olNs.Logon(Missing.Value, Missing.Value, false, true);

            //pick a MAPI folder
            Outlook.MAPIFolder publicFolders = (Outlook.MAPIFolder) olNs.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders).Folders["Food"];

            try
            {
                foreach (Outlook.MAPIFolder f in publicFolders.Folders)
                {
                    if (f.Name == "Burgers")
                        textBlockCount = f.Items.Count.ToString();
                }
                return;
            }
            catch (System.Exception e)
            {
                Log.Logging(e, "Mailbox exception");
            }

这是一个经过编辑的xml文件,用虚拟数据替换了所有客户详细信息:

<?xml version="1.0"?>
<XMLCONTAINER version="1.0" incCount="3">
  <APPLICATION id="APPLICATION_NODE_1" title="" name="55038170812101005FOODITEMS Foodpayment" application="Excel" appfile="%TURTLE%\xmlsForms\RDBxmls.xla" command="openRDBeDesigns">
    <MAINDATA id="CONTAINER_NODE_2" title=""/>
    <TURTLE.FORM id="CONTAINER_NODE_3" title="" table="tblMain" database="\\c\g\FDR1\FOODS\Data\Link\FOODpayments.mdb">
      <FIELD fieldName="EID">1234567</FIELD>
      <FIELD fieldName="DateTime">21/02/2019 14:50:17</FIELD>
      <FIELD fieldName="DateOfLetter">21/02/19</FIELD>
      <FIELD fieldName="Name">Mr Joe Blogs</FIELD>
      <FIELD fieldName="CUSTID">RR123456R</FIELD>
      <FIELD fieldName="paymentAmount">1144.80</FIELD>
      <FIELD fieldName="AmountForPreviousYears">1144.80</FIELD>
      <FIELD fieldName="NoPurple">i&amp;e not comp'd - so he can't pay</FIELD>
      <FIELD childID="CONTAINER_NODE_3_field59option1" fieldName="ComeBackReason">Sourcing2</FIELD>
      <FIELD fieldName="Telephone">012345678</FIELD>
      <FIELD fieldName="AcceptPhoneNumber">TRUE</FIELD>
      <FIELD childID="CONTAINER_NODE_3_field7option4" fieldName="CustomerType">4</FIELD>
      <FIELD fieldName="Target4">FALSE</FIELD>
      <FIELD fieldName="Card">FALSE</FIELD>
      <FIELD fieldName="ContractSettlement">FALSE</FIELD>
      <FIELD fieldName="UPYear">512</FIELD>
      <FIELD fieldName="AllocatedTo">1234567</FIELD>
      <FIELD fieldName="LateCase">FALSE</FIELD>
    </TURTLE.FORM>
  </APPLICATION>
</XMLCONTAINER>

所以我一直在更新它,发现PostItem有效而MailItem无效。现在,我可以显示消息正文,那么如何将这些FIELDS提取到列表或其他内容中?我输入了字符串mystring,但看起来不像它保存的那样:

for (int i = 1; i < publicFolder.Items.Count; i++)
                {
                    item = (PostItem)publicFolder.Items[i];
                    if (item != null)
                    {
                        MessageBox.Show(item.Subject, "Subject");
                        MessageBox.Show(item.BodyFormat.ToString(), "Item: {0}");
                        MessageBox.Show(item.Body, "Body");

                        string mystring = item.Body;



                    }
                    else
                    {
                        MessageBox.Show("No mail items found");
                    }
                }

2 个答案:

答案 0 :(得分:0)

使用olNs.OpenSharedItem打开独立的MSG文件。

答案 1 :(得分:0)

我解析了一些xml。不知道你需要什么。我使用了Net Library 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 xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);

            var results = doc.Descendants("FIELD").Select(x => new {
                fieldName = (string)x.Attribute("fieldName"),
                childID = (string)x.Attribute("childID"),
                value = (string)x
            }).ToList();
        }
    }
}