如何从Outlook 2010的加载项c#写入xml文件? [英] How can I write to the xml file from my add-in c# for outlook 2010 ?

查看:102
本文介绍了如何从Outlook 2010的加载项c#写入xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的插件c#中为outlook 2010写入xml文件。



我使用的是VSTO 2010.



MyFile.config:

 <? xml version =   1.0 encoding =   utf-8 ?>  
< 配置 >
< appSettings >
< add key = Test1 value = 我的值1 / >
< / appSettings >
< / configuration >





所以我想从Outlook 2010的加载项c#中更改Test1(key =Test1)的值。



MyAdd-in.cs



  private   void  ThisAddIn_Startup( object  sender,System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Assembly.GetExecutingAssembly()。Location + 。config);

// 写入MyFile.config

// 检索appSettings节点
XmlNode node = doc.SelectSingleNode( // appSettings);

// 选择包含密钥的'add'元素
XmlElement elem =(XmlElement)node.SelectSingleNode( string .Format( // add [@key ='{0}'] Test1 ));
if (elem!= null
{
// 为键添加值
elem.SetAttribute( value TestValue< /跨度>);
}
其他
{
// < span class =code-comment>找不到键,因此创建'add'元素并设置它的键/值属性

elem = doc.CreateElement( add);
elem.SetAttribute( key Test1);
elem.SetAttribute( value TestValue);
node.AppendChild(elem);
}

doc.Save(Assembly.GetExecutingAssembly()。Location + 的.config);
}





所以它不起作用,问题是[Assembly.GetExecutingAssembly()。 +。config] !!!



能帮帮我吗?



谢谢你提前。

解决方案

您好,



更改应用配置



配置config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.Settings [ test]。Value = blah;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection( appSettings);





但是在你使用插件的情况下我认为在AppData中保存JSON / XML会更适用和裂变。



如何做...



获取AppData文件夹



 var folderName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),OutlookAddin); 





创建文件夹(如果不存在)现在在那里创建您想要的XML文件或JSON文件并进行读写。我建议你使用序列化来创建或修改AppData文件夹中的配置细节。



如何序列化类:使用C#进行对象序列化 [ ^ ]



使用JSON: ASP.NET中的JSON序列化和反序列化 [ ^ ](看看如何将.Net类序列化为JSON )



我忙于部署在我的最后这就是为什么我没有回复你的答案抱歉延迟。



如果您需要进一步说明,请随时询问。

I want to write to the xml file from my add-in c# for outlook 2010.

I use VSTO 2010.

MyFile.config :

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="Test1" value="My value 1"/>
        </appSettings>
    </configuration>



So I want to change the value of Test1 (key = "Test1") from my add-in c# for outlook 2010.

MyAdd-in.cs

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(Assembly.GetExecutingAssembly().Location + ".config");

    // To write in MyFile.config

    // retrieve appSettings node
    XmlNode node = doc.SelectSingleNode("//appSettings");

    // Select the 'add' element that contains the key
    XmlElement elem =  (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", "Test1"));
    if (elem != null)
    {
        // add value for key
        elem.SetAttribute("value", "TestValue");
    }
    else
    {
        // Key was not found so create the 'add' element and set it's key/value attributes
        elem = doc.CreateElement("add");
        elem.SetAttribute("key", "Test1");
        elem.SetAttribute("value", "TestValue");
        node.AppendChild(elem);
     }

    doc.Save(Assembly.GetExecutingAssembly().Location + ".config");
}



So it doesn't work and the problem is [Assembly.GetExecutingAssembly().Location + ".config"] !!!

Can you help me please ?

Thank you in advance.

解决方案

Hi,

To Change App Config

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
    config.AppSettings.Settings["test"].Value = "blah";       
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");



but In your case as you are using Add-In I think Save JSON/XML in AppData would be more applicable and fissile.

How to do it...

get AppData Folder

var folderName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),"OutlookAddin");



Create folder if don't exists now create your desire XML File or JSON file there and Read and write. I would suggest you to use serialization to create or modify those configuration details at AppData folder.

How to Serialize Class: Object Serialization using C#[^]

using JSON : JSON Serialization and Deserialization in ASP.NET[^] ( Just watch how to serialize a .Net class to JSON)

I was busy with some deployment at my end that's why I did not reply your answer sorry for delay.

If you need any further clarification please feel free to ask.


这篇关于如何从Outlook 2010的加载项c#写入xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆