2

I have a class object:

[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; }
}

And I have data already present like this:

test:
    id=1
    Name="abc"
    locTags
       tag
           tagId=1
           tagTitle="xyz"

    id=2
    name="qwe"
    ...

I would like to test=1 add new object to Tag, should get result:

test:
    id=1
    Name="abc"
    locTags
        tag
            tagId=1
            tagTitle="xyz"

            tagId=2
            tagTitle="pqr"
     id=2
     name="qwe"
     ...

How do I do that?

Edit

List<Tag> tagNew = test.locTags.Tag.ToList();
tagNew.Add(new Tag
{
    TagTitle = "pqr",
    TagId = "2"
});

test.locTags.Tag = tagNew;

but the last line gives me error:

Error 10 Cannot implicitly convert type 'System.Collections.Generic.List' to 'Tag[]'

4
  • How about making Tag[] Tag in LocTasks as List<Tag> and then go by Tag.Add(objnewTag) ? Commented May 9, 2016 at 6:55
  • Or are you struggling to find out which tag should be added to which test? Commented May 9, 2016 at 7:17
  • @kassi Thanks for quick response. I tried to do it as you said check my edit. Getting error while trying put that back to existing object Commented May 9, 2016 at 7:19
  • 3
    Try test.locTags.Tag = tagNew.ToArray(); Commented May 9, 2016 at 7:26

1 Answer 1

3

Make Tag[] Tag as List<Tag> and then use test.LocTagXY.Tags.Add(newTag)

If you wish to stay with Arrays, use Pradeep Kumar's test.locTags.Tag = tagNew.ToArray()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.