Hi, there,
I need to read an XML file. Actually it is AddIn file in XML format for
Add-Ins in VS2005. The standard file is shown next.
My task is to deserialize it, change some element, and serialize it. I got
two problems in doing so:
1. It seems to me my code didn't get the hostapplication array.
2. How to serialize it back with the changed field?
Please help! My class definition, code to deserialize, serialize are
following:
[XmlRoot("Extensibility", Namespace =
"http://schemas.microsoft.com/AutomationExtensibility",
IsNullable = false)]
public class XMLAddIn
{
public Host[] HostApplications;
public AddIn Addin;
}
public class Host
{
public string Name;
public string Version;
}
public class AddIn
{
public string FriendlyName;
public string Description;
public string Assembly;
public string FullClassName;
public string LoadBehavior;
public string CommandPreload;
public string CommandLineSafe;
}
-----------------------Deserialize and
serialize----------------------------------
XmlSerializer serializer = new XmlSerializer(typeof(XMLAddIn));
FileStream fs = new FileStream(strAddInFile, FileMode.Open);
XMLAddIn xAddIn = (XMLAddIn)serializer.Deserialize(fs);
//change the assembly path
xAddIn.Addin.Assembly = strAssmbly;
serializer.Serialize(fs, xAddIn);
//a problem here is that the changed file like some mess code appended to my
old file. I guess it is due to my file open mode. I changed the file name,
using another writer stream, but got only partial XMLAddIn file. The
HostApplication array are apparently not read in.
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<Extensibility
xmlns="http://schemas.microsoft.com/AutomationExtensibility">
<HostApplication>
<Name>Microsoft Visual Studio Macros</Name>
<Version>8.0</Version>
</HostApplication>
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>8.0</Version>
</HostApplication>
<Addin>
<FriendlyName>My great new add-in.</FriendlyName>
<Description>This add-in does it all.</Description>
<AboutBoxDetails>Copyright 2005.</AboutBoxDetails>
<AboutIconData>0000 . . . FFFF0000</AboutIconData>
<Assembly>MyNewAddin.dll</Assembly>
<FullClassName>MyNewAddin.Connect</FullClassName>
<LoadBehavior>1</LoadBehavior>
<CommandPreload>1</CommandPreload>
<CommandLineSafe>0</CommandLineSafe>
</Addin>
</Extensibility>