如何使用 LINQ 将 xml 文件转换为对象
我有以下 xml 文件来生成我们网站的菜单。
<xs:element name="Menu">
<xs:complexType>
<xs:sequence>
<xs:element name="MenuItem" type="MenuItemType" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attribute name="Title" type="xs:string"></xs:attribute>
<xs:attribute name="Type" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="MenuItemType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MenuItem" type="MenuItemType" />
</xs:choice>
<xs:attribute name="Text" type="xs:string"></xs:attribute>
<xs:attribute name="Url" type="xs:string"></xs:attribute>
</xs:complexType>
现在我正在使用 xmlserializer 将这些 xml 文件转换为 Menu 对象并使用它们生成菜单。我想使用 LINQ to xml 将这些 xml 文件转换为同一对象。任何帮助将不胜感激。上述 xml 文件生成的类是
public partial class Menu {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MenuItem")]
public MenuItemType[] MenuItem;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Title;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type;
}
public partial class MenuItemType {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MenuItem")]
public MenuItemType[] Items;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Text;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Url;
}
I have xml files with following to generate the menu for our web site.
<xs:element name="Menu">
<xs:complexType>
<xs:sequence>
<xs:element name="MenuItem" type="MenuItemType" maxOccurs="unbounded"></xs:element>
</xs:sequence>
<xs:attribute name="Title" type="xs:string"></xs:attribute>
<xs:attribute name="Type" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="MenuItemType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="MenuItem" type="MenuItemType" />
</xs:choice>
<xs:attribute name="Text" type="xs:string"></xs:attribute>
<xs:attribute name="Url" type="xs:string"></xs:attribute>
</xs:complexType>
Right now I am using xmlserializer to convert these xml files in to Menu objects and use them to generate the menu. I want to use LINQ to xml to convert these xml files in to same object. Any help will be appreciated. Generated class for above xml file is
public partial class Menu {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MenuItem")]
public MenuItemType[] MenuItem;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Title;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type;
}
public partial class MenuItemType {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MenuItem")]
public MenuItemType[] Items;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Text;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Url;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有测试过。但是,希望这能起作用。
I haven't tested it. But, hope this works.