我有一个类对象:
[XmlRoot(ElementName = "Tag")]
public class Tag
{
[XmlElement(ElementName = "TagId")]
public string TagId { get; set; }
[XmlElement(ElementName = "TagTitle")]
public string TagTitle { get; set; }
}
[XmlRoot(ElementName = "LocTags")]
public class LocTags
{
[XmlElement(ElementName = "Tag")]
public Tag[] Tag { get; set; }
}
[XmlRoot(ElementName = "test")]
public class test
{
[XmlElement(ElementName = "ID")]
public string ID { get; set; }
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "LocTags")]
public LocTags LocTags { get; set; }
}
我的数据已经存在:
test:
id=1
Name="abc"
locTags
tag
tagId=1
tagTitle="xyz"
id=2
name="qwe"
...
我想测试= 1将新对象添加到Tag,应该得到结果:
test:
id=1
Name="abc"
locTags
tag
tagId=1
tagTitle="xyz"
tagId=2
tagTitle="pqr"
id=2
name="qwe"
...
我该怎么做?
修改
List<Tag> tagNew = test.locTags.Tag.ToList();
tagNew.Add(new Tag
{
TagTitle = "pqr",
TagId = "2"
});
test.locTags.Tag = tagNew;
但是最后一行给了我错误:
错误10无法隐式转换类型&#39; System.Collections.Generic.List&#39;到&#39;标记[]&#39;
答案 0 :(得分:3)
将Tag[]
标记为List<Tag>
,然后使用test.LocTagXY.Tags.Add(newTag)
如果您希望继续使用阵列,请使用Pradeep Kumar test.locTags.Tag = tagNew.ToArray()