0

I have a table in SQL Server that has a XML type column.

That column has dynamic value in XML type, I want read it table in C#.

How can we separate each property in my XML?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is the question how you can read in xml from a SQL table, or is it something else? Can you post a code snippet of what you want to do. – Niels Berglund Jul 12 '17 at 06:00
  • no something else. i can read xml. in xml type there are a xml node with deffrent lenght because it feild of database create by portal view.i need handle deffrent type of each record in c# . for example one record have 20 property in xml feild and other record have 50 property in xml feild – Steve M.Jalili Jul 12 '17 at 06:06
  • Unless I misunderstand you, you'd have to loop through your nodes/elements in the returned xml, and do whatever you need to do with it. To see an example, look at this stackoverflow [question and answer](https://stackoverflow.com/questions/2915294/iterating-through-all-nodes-in-xml-file). – Niels Berglund Jul 12 '17 at 06:19

1 Answers1

0

Get the column from database like this. CodeAndPassword(string) is class's property.

CodeAndPassword = Serializer.DeSerialize<Config>(row.Field<string>("Explanation"));

And your DeSerialize function sholud be like this

public static string DeSerialize<T>(string xml)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = false;
    xmlDoc.LoadXml(xml);

    Config config = Serializer.DeSerialize<Config>(xmlDoc.DocumentElement);
    string code = config.Login.Code;
    string password = config.Login.Pass;

    return code + "+" + password;
} 

And here is our Config Class

[Serializable]
public class Config
{
    public ConfigLogin Login { get; set; }
}

[XmlRoot("Login")]
public class ConfigLogin
{
    public string Code { get; set; }
    public string Pass { get; set; }
}
Kemal Güler
  • 608
  • 1
  • 6
  • 21
  • that is OK but i it is not my problem, i designed an Portal database.For create a database you can have any tables in your database but now i have an database with 5 table that an table of it can save template of many tables inside itself,finally now i can not detect property of each table on XML property because each filed of row is dynamic – Steve M.Jalili Jul 12 '17 at 07:24
  • Maybe you can check it like if(row["myColumn"].Contains('<')) @SteveM.Jalili – Kemal Güler Jul 12 '17 at 07:35