0

I have this Style XML:

<Style Name="TextBox2" Type="TEXT">
        <Alignment>LEFT</Alignment>
        <BorderColor>blue</BorderColor>
        <BorderStyle>Solid</BorderStyle>
        <BackGroundColor>red</BackGroundColor>
        <Options>VISIBLE</Options>
        <TextColor>Black</TextColor>
        <TextSize>10</TextSize>
        <MaxCharacterLength>50</MaxCharacterLength>

I want to write a function which enables me to search for a Name (e.g. Checkbox2) and returns us corresponding names and values of child nodes (Alignment=Center, Bordercolor=red etc.) Should I use datatable or Dataset? Or any suggestion will be much appreciated

1 Answers1

0

Try something like this to get only one object

            string xml =
            "<Root>" +
                "<Style Name=\"TextBox2\" Type=\"TEXT\">" +
                    "<Alignment>LEFT</Alignment>" +
                    "<BorderColor>blue</BorderColor>" +
                    "<BorderStyle>Solid</BorderStyle>" +
                    "<BackGroundColor>red</BackGroundColor>" +
                    "<Options>VISIBLE</Options>" +
                    "<TextColor>Black</TextColor>" +
                    "<TextSize>10</TextSize>" +
                    "<MaxCharacterLength>50</MaxCharacterLength>" +
                "</Style>" +
            "</Root>";

            XDocument doc = XDocument.Parse(xml);

            var style = doc.Descendants("Style").Where(x => (string)x.Attribute("Name") == "TextBox2").Select(x => new
            {
                type = (string)x.Attribute("Type"),
                alignment = (string)x.Element("Alignment"),
                borderColor = (string)x.Element("BorderColor"),
                borderStyle = (string)x.Element("BorderStyle"),
                background = (string)x.Element("BackGroundColor"),
                options = (string)x.Element("Options"),
                textColor = (string)x.Element("TextColor"),
                textSize = (string)x.Element("TextSize"),
                maxCharacters = (int)x.Element("MaxCharacterLength"),
            }).FirstOrDefault();
jdweng
  • 33,250
  • 2
  • 15
  • 20