2
我有一類對象:如何對象添加到現有的類對象
[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添加新對象到標籤,應得到結果:
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;
但最後一行給我的錯誤:
Error 10 Cannot implicitly convert type 'System.Collections.Generic.List' to 'Tag[]'
如何將LocTasks中的Tag []標籤設置爲列表,然後通過Tag.Add(objnewTag)進行操作? –
kassi
或者你正在努力找出哪個標籤應該添加到哪個測試? – kassi
@kassi感謝您的快速響應。我試圖按照你所說的去檢查我的編輯。嘗試將錯誤放回到現有對象時出錯 – Arti