从xml </value> </attribute>获取字符串<attribute> = <value>

时间:2014-04-13 20:26:41

标签: c# xml

我有一个问题,如果有代码从xml读取设置,例如:

<length>5</length>
<height>10</heigth>

我如何进入C#

for (i = 0; i < atributesInFile; i++)
{
   <atribute> = <atribute>.innertext;
}

所以它会像

length = 5;
height = 10;

我希望你明白我的观点:)

2 个答案:

答案 0 :(得分:0)

您可以声明一个代表XML的类:

    public class MyType
    {
      public int Height { get; set; }
      public int Length { get; set; }
    }

然后这样做:

      var xmlSerializer = new XmlSerializer(typeof (MyType));
      using (var reader = new XmlTextReader(@"location"))
      {
          var myType = (MyType) xmlSerializer.Deserialize(reader);
      }

答案 1 :(得分:0)

如果你的XML是这样的

<settings>
 <length>5</length>
 <height>10</height>
</settings>

你可以做到

var xdoc = XDocument.Load(pathtoxml);

foreach (var setting in xdoc.Descendants("settings"))
{
 var line = setting.Name + " = " + setting.Value + ";";
 // display line
}