0

我有这个样式 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>

我想编写一个函数,使我能够搜索名称(例如 Checkbox2)并向我们返回子节点的相应名称和值(Alignment=Center、Bordercolor=red 等)。我应该使用数据表还是数据集?或任何建议将不胜感激

4

1 回答 1

0

尝试这样的事情只得到一个对象

            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();
于 2016-10-10T09:32:55.647 回答