xsd2code - problem deserializing xml file
In the past I created own xml serialization class manually. But now I would like to do it automatically. So I found XSD tool by Microsoft. Unfortunately I face a bug with this utility (CS030 etc.). But next I found looking great open source tool XSD2CODE http://xsd2code.codeplex.com/
Everything was looking great until I spotted that not all my xml data has been deserialized (lol).
My XML:
<?xml version="1.0" encoding="utf-8" ?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AdminPassword>Admn1234</AdminPassword>
<ImagesFolder></ImagesFolder>
<Resolutions>
<Resolution id="1024x768">
<Width>1024</Width>
<Height>768</Height>
<Panel id="top">
<Height>603</Height>
<LocationOnMainForm>
<X>0</X>
<Y>0</Y>
</LocationOnMainForm>
<Background>1_panel_top_background.jpg</Background>
<Buttons>
<Button id="pbMainTopComp">
<Background>panel_top_btn_info.jpg</Background>
<Location>
<X>27</X>
<Y>123</Y>
</Location>
<OnClickOpenLink>http://www.google.com</OnClickOpenLink>
</Button>
<Button id="pbMainTopSelf">
<Background>panel_top_btn_self.jpg</Background>
<Location>
<X>360</X>
<Y>123</Y>
</Location>
<OnClickOpenLink>http://www.yahoo.com</OnClickOpenLink>
</Button>
</Buttons>
</Panel>
<Panel id="bottom">
<LocationOnMainForm>
<X>0</X>
<Y>603</Y>
</LocationOnMainForm>
<Height>165</Height>
<Background>panel_bottom_background.jpg</Background>
<Buttons>
<Button id="pbMainBottomPages">
<Background>panel_bottom_btn_pages.jpg</Background>
<Location>
<X>38</X>
<Y>39</Y>
</Location>
<OnClickOpenLink/>
</Button>
<Button id="pbMainBottomReport">
<Background>panel_bottom_btn_report.jpg</Background>
<Location>
<X>344</X>
<Y>39</Y>
</Location>
<OnClickOpenLink>http://www.bing.com</OnClickOpenLink>
</Button>
</Buttons>
</Panel>
</Resolution>
</Resolutions>
</MySettings>
When I use standard method to deserialize I have error.
MySettings mySettings = new MySettings();
using (TextReader textReader = new StreamReader(@"settings_test.xml"))
{
XmlSerializer xSerializer = new XmlSerializer(typeof(MySettings));
mySettings = (MySettings)xSerializer.Deserialize(textReader);
}
When I use Xsd2Code built in deserialize method:
MySettings mySettings = new MySettings();
bool isOk = MySettings.LoadFromFile(@"settings_test.xml", out mySettings);
string pass = mySettings.AdminPassword;
foreach (MySettingsResolutionsResolution item in mySettings.Resolutions)
{
string height = item.Height;
foreach (MySettingsResolutionsResolutionPanel panel in item.Panel)
{
string bckg = panel.开发者_运维百科Background;
foreach (MySettingsResolutionsResolutionPanelButtonsButton btn in panel.Buttons)
{
string btnBckg = btn.Background;
}
}
}
then I have no errors but not all data are deserialized from xml. For example:
My Xsd2Code settings:
Can anyone advice me:
- What I did wrong and how to correct this?
- or
- Propose another automated working solution for my XML (above XML).
Thanks a lot in advance for all suggestions.
In my case it was adding order parameters to the XmlElementAttribute
(i.e. Order = 4
).
If you remove the order parameter it works just fine. What I can't find is a way to prevent XSD2CODE
from including the order parameter when it generates the class.
Several issues can cause this situation:
- Invalid order of elements in your xml within block then if you actually do not care about the order just replace it with element in xsd (it will remove the XmlElementAttribute from the generated code from KenE answer).
- Or more simple case is a mistake in xml for example addition of to existing element (xsd2code will not fail during Desalinization but will ignore the element)
Hope it helps
精彩评论