Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "deep" XML with MSTest XML Datasource

I'm having some problems with MSTest using an XML Datasource. Assume that I've got an XML file that looks like this:

<Users>
    <User>
        <Id>1</Id>
        <Name>
            <First>Mike</First>
            <Last>Paterson</Last>
        </Name>
    </User>
    <User>
        <Id>2</Id>
        <Name>
            <First>John</First>
            <Last>Doe</Last>
        </Name>
    </User>
</Users>

My problem, however, is I can't get ahold of the Name element:

var name = row["Name"];
System.ArgumentException: Column 'Name' does not belong to table User.

I suppose this might be more of a DataRow question but any help would really be appreciated.

EDIT:

Even if I copy the DataRow to a new DataTable and write the XML the Name element is not present:

[DeploymentItem("XmlDatasourceTest\\Users.xml"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\Users.xml", "User", DataAccessMethod.Sequential), TestMethod]
public void TestMethod1()
{
    var row = TestContext.DataRow;

    DataTable table = row.Table.Copy();

    foreach (DataRow r in table.AsEnumerable().ToArray())
    {
        r.Delete();
    }

    table.ImportRow(row);

    table.WriteXml(@"C:\test.xml");
}

For the first row, this yields:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <User>
    <Id>1</Id>
  </User>
</DocumentElement>
like image 232
devlife Avatar asked Oct 11 '11 12:10

devlife


1 Answers

I've also faced such problem and this is how I was able to solve it:

DataRow dataRow = TestContext.DataRow.GetChildRows("User_Name").First();
string s = (string)dataRow["First"]; // s = "Mike"

"User_Name" -- is the name of child relation that binds User table and Name table.

like image 57
DVG Avatar answered Sep 23 '22 12:09

DVG