序列化时根重复 [英] Duplicated root when serializing
问题描述
我正在尝试将字典序列化为 xml,但是 xml 的结构与我想要的不同.出于某种原因,我没有在该根中拥有一个根和多个子级,而是为字典中的每个键和值获得一个新根.此外,字典中的每个 KeyValuePair 都会显示消息框,我希望在导出字典时显示一次消息框.我将 Microsoft Visual Studio 2010 与 C# 以及从 Windows 窗体上的文本框中收集的字典中的所有值一起使用.
I am trying to serialize a dictionary into xml, but the structure of the xml is different to the one I would like. For some reason instead of having one root and multiple child within that root I get a new root for each key and value in the dictionary. Also the message box is being shown for each KeyValuePair within the dictionary, I would like the message box to be shown once when the dictionaries were exported. I am using Microsoft Visual Studio 2010 with C# and all values within the dictionary collected from text boxes on a windows form.
这是当前 xml 布局的样子:
This is how the current xml layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<Coordinate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Date_Time>10/02/2015 11/17/00</Date_Time>
<BeaconID>1</BeaconID>
<X_Coordinate>2</X_Coordinate>
<Y_Coordinate>3</Y_Coordinate>
</Coordinate><?xml version="1.0" encoding="utf-8"?>
<Coordinate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Date_Time>10/02/2015 11/17/01</Date_Time>
<BeaconID>2</BeaconID>
<X_Coordinate>3</X_Coordinate>
<Y_Coordinate>4</Y_Coordinate>
</Coordinate>
这就是我想要的样子:
<?xml version="1.0" encoding="utf-8"?>
<Coordinates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Coordinate>
<Date_Time>10/02/2015 11/17/00</Date_Time>
<BeaconID>1</BeaconID>
<X_Coordinate>2</X_Coordinate>
<Y_Coordinate>3</Y_Coordinate>
</Coordinate>
<Coordinate>
<Date_Time>10/02/2015 11/17/01</Date_Time>
<BeaconID>2</BeaconID>
<X_Coordinate>3</X_Coordinate>
<Y_Coordinate>4</Y_Coordinate>
</Coordinate>
</Coordinates>
这是我所有的xml相关代码:
This is all of my xml related code:
public void btn_Submit_Click(object sender, EventArgs e)
{
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
SaveValues(v);
}
}
public class Coordinate//Root element
{
public string Date_Time { get; set; }
public string BeaconID { get; set; }
public string X_Coordinate { get; set; }
public string Y_Coordinate { get; set; }
}
public void SaveValues(Coordinate v)
{
XmlSerializer serializer = new XmlSerializer(typeof(Coordinate));
using (TextWriter textWriter = new StreamWriter(@"F:\Vista\Exporting into XML\Test1\Coordinates output.xml", true))
{
serializer.Serialize(textWriter, v);
}
MessageBox.Show("Coordinates were exported successfully", "Message");//Let the user know the export was succesfull
}
你能告诉我我做错了什么吗?如果我遗漏了任何东西,或者我不够清楚,请告诉我,我会提供帮助.我是 XML 和序列化的新手,因此不胜感激.
Can you tell me what I am doing wrong. If I am missing anything or I just wasn't clear enough just let me know it and I will help with it. I am new to XML and to serialization so any help would be appreciated.
更新:
@PK更好的解释:
<?xml version="1.0" encoding="utf-8"?>
<Coordinates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Coordinate>
<Coordinate>
<DateTime>10/02/2015 02/35/14</DateTime>
<BeaconID>1</BeaconID>
<XCoordinate>2</XCoordinate>
<YCoordinate>3</YCoordinate>
</Coordinate>
<Coordinate>
<DateTime>10/02/2015 02/35/14</DateTime>
<BeaconID>2</BeaconID>
<XCoordinate>3</XCoordinate>
<YCoordinate>4</YCoordinate>
</Coordinate>
</Coordinate>
</Coordinates>
代替:
<?xml version="1.0" encoding="utf-8"?>
<Coordinates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Coordinate>
<DateTime>10/02/2015 02/35/14</DateTime>
<BeaconID>1</BeaconID>
<XCoordinate>2</XCoordinate>
<YCoordinate>3</YCoordinate>
</Coordinate>
<Coordinate>
<DateTime>10/02/2015 02/35/14</DateTime>
<BeaconID>2</BeaconID>
<XCoordinate>3</XCoordinate>
<YCoordinate>4</YCoordinate>
</Coordinate>
</Coordinates>
推荐答案
可以试试下面的代码吗?
Could you please try below code?
public void btn_Submit_Click(object sender, EventArgs e)
{
List<KeyValuePair<string, Tuple<int, int>>> d = new List<KeyValuePair<string, Tuple<int, int>>>
{
new KeyValuePair<string, Tuple<int, int>>("1",Tuple.Create<int,int>(1,1)),
new KeyValuePair<string, Tuple<int, int>>("2",Tuple.Create<int,int>(2,2)),
new KeyValuePair<string, Tuple<int, int>>("3",Tuple.Create<int,int>(3,3)),
new KeyValuePair<string, Tuple<int, int>>("4",Tuple.Create<int,int>(4,4)),
new KeyValuePair<string, Tuple<int, int>>("5",Tuple.Create<int,int>(5,5))
};
List<Coordinate> cv = new List<Coordinate>();
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
}
SaveValues(cv);
}
public void SaveValues(List<Coordinate> v)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Coordinate>), new XmlRootAttribute("Coordinates"));
using (TextWriter textWriter = new StreamWriter(@"F:\Vista\Exporting into XML\Test1\Coordinates output.xml", true))
{
serializer.Serialize(textWriter, v);
}
MessageBox.Show("Coordinates were exported successfully", "Message");//Let the user know the export was succesfull
}
更新代码以包含字典...
Updated code to have dictionary...
public void btn_Submit_Click(object sender, EventArgs e)
{
Dictionary<string, Tuple<int, int>> d = new Dictionary<string, Tuple<int, int>>();
int X_Co = 1;
int Y_Co = 1;
var t = new Tuple<int, int>(X_Co, Y_Co);
d.Add("1", t);
X_Co = 2;
Y_Co = 2;
t = new Tuple<int, int>(X_Co, Y_Co);
d.Add("2", t);
List<Coordinate> cv = new List<Coordinate>();
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
}
SaveValues(cv);
}
更新了代码,每个循环都有单独的文件.
Updated code to have separate files for every loop.
foreach (KeyValuePair<string, Tuple<int, int>> entry in d)
{
List<Coordinate> cv = new List<Coordinate>();
Coordinate v = new Coordinate();
v.Date_Time = DateTime.Now.ToString("dd/MM/yyyy hh/mm/ss");
v.BeaconID = entry.Key;
v.X_Coordinate = entry.Value.Item1.ToString();
v.Y_Coordinate = entry.Value.Item2.ToString();
cv.Add(v);
SaveValues(cv);
}
这篇关于序列化时根重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!