Linq to XML 数据结构
Linq to XML data structure
这是我的 XML:
<home>
<contents>
<row>
<content>
<idContent>1</idContent>
<title>title1</title>
</content>
<content>
<idContent>2</idContent>
<title>title2</title>
</content>
</row>
<row>
<content>
<idContent>3</idContent>
<title>title3</title>
</content>
<content>
<idContent>4</idContent>
<title>title4</title>
</content>
</row>
</contents>
我想将此信息存储在对象列表中
列表 myList = ...
内容可能是:
int idContent;
string title;
int row_number;
每个内容对象都必须在 XML 中存储它所在的行。
执行此操作的最佳方法是什么?
我能想到的最佳解决方案 - 虽然它没有使用 Linq to XML - 是将你的 XML 文档输入到各种 XML 模式生成器之一,例如 http://www.freeformatter.com/xsd-generator.html and pipe this generated Schema right into xsd.exe (see https://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx).生成的代码可用于读取(和写入)XML 文档,例如您提供的文档,您当然可以在此集合上应用 Linq to Objects。
但是,由于您提到的 row_number 字段尚未出现在您的示例 XML 中,您必须将其添加到 XML 或手动编辑 XML 之后的模式。
假设 row_number
只是一个与它在 XML 中出现的顺序相关的序列,那么你可以这样做:
var doc = XDocument.Parse(xml);
var contents = doc.Descendants("row")
.Select((e, index) => new {Row = e, RowIndex = index})
.SelectMany(x => x.Row.Elements("content").Select(e => new {Content = e, x.RowIndex}))
.Select(x => new Content
{
IdContent = (int)x.Content.Element("idContent"),
Title = (string)x.Content.Element("title"),
RowNumber = x.RowIndex + 1
}).ToList();
我在类似情况下使用此方法:
public static Object CreateObject(string XMLString, Object YourClassObject)
{
System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(YourClassObject.GetType());
//The StringReader will be the stream holder for the existing XML file
YourClassObject = oXmlSerializer.Deserialize(new System.IO.StringReader(XMLString));
//initially deserialized, the data is represented by an object without a defined type
return YourClassObject;
}
使用此方法,您可以从 XML 字符串创建 class 对象。我没有在你的场景中测试它,但你可以在以下对象上使用它 home
class:
public class home
{
public List<row> contents;
}
public class row
{
public List<content> content;
}
public class content
{
public int idContent;
public string title;
}
用法:
home h = new home();
h = (home)CreateObject(xml, h);
请记住,变量和 class 名称必须与 XML 节点的名称完全相同。
额外:
如果要将 class 对象转换为 XML 字符串,请使用此方法:
string CreateXML(Object YourClassObject)
{
XmlDocument xmlDoc = new XmlDocument(); //Represents an XML document,
// Initializes a new instance of the XmlDocument class.
XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());
// Creates a stream whose backing store is memory.
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, YourClassObject);
xmlStream.Position = 0;
//Loads the XML document from the specified string.
xmlDoc.Load(xmlStream);
return xmlDoc.InnerXml;
}
}
这是我的 XML:
<home>
<contents>
<row>
<content>
<idContent>1</idContent>
<title>title1</title>
</content>
<content>
<idContent>2</idContent>
<title>title2</title>
</content>
</row>
<row>
<content>
<idContent>3</idContent>
<title>title3</title>
</content>
<content>
<idContent>4</idContent>
<title>title4</title>
</content>
</row>
</contents>
我想将此信息存储在对象列表中
列表 myList = ...
内容可能是:
int idContent;
string title;
int row_number;
每个内容对象都必须在 XML 中存储它所在的行。
执行此操作的最佳方法是什么?
我能想到的最佳解决方案 - 虽然它没有使用 Linq to XML - 是将你的 XML 文档输入到各种 XML 模式生成器之一,例如 http://www.freeformatter.com/xsd-generator.html and pipe this generated Schema right into xsd.exe (see https://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx).生成的代码可用于读取(和写入)XML 文档,例如您提供的文档,您当然可以在此集合上应用 Linq to Objects。
但是,由于您提到的 row_number 字段尚未出现在您的示例 XML 中,您必须将其添加到 XML 或手动编辑 XML 之后的模式。
假设 row_number
只是一个与它在 XML 中出现的顺序相关的序列,那么你可以这样做:
var doc = XDocument.Parse(xml);
var contents = doc.Descendants("row")
.Select((e, index) => new {Row = e, RowIndex = index})
.SelectMany(x => x.Row.Elements("content").Select(e => new {Content = e, x.RowIndex}))
.Select(x => new Content
{
IdContent = (int)x.Content.Element("idContent"),
Title = (string)x.Content.Element("title"),
RowNumber = x.RowIndex + 1
}).ToList();
我在类似情况下使用此方法:
public static Object CreateObject(string XMLString, Object YourClassObject)
{
System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(YourClassObject.GetType());
//The StringReader will be the stream holder for the existing XML file
YourClassObject = oXmlSerializer.Deserialize(new System.IO.StringReader(XMLString));
//initially deserialized, the data is represented by an object without a defined type
return YourClassObject;
}
使用此方法,您可以从 XML 字符串创建 class 对象。我没有在你的场景中测试它,但你可以在以下对象上使用它 home
class:
public class home
{
public List<row> contents;
}
public class row
{
public List<content> content;
}
public class content
{
public int idContent;
public string title;
}
用法:
home h = new home();
h = (home)CreateObject(xml, h);
请记住,变量和 class 名称必须与 XML 节点的名称完全相同。
额外:
如果要将 class 对象转换为 XML 字符串,请使用此方法:
string CreateXML(Object YourClassObject)
{
XmlDocument xmlDoc = new XmlDocument(); //Represents an XML document,
// Initializes a new instance of the XmlDocument class.
XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());
// Creates a stream whose backing store is memory.
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, YourClassObject);
xmlStream.Position = 0;
//Loads the XML document from the specified string.
xmlDoc.Load(xmlStream);
return xmlDoc.InnerXml;
}
}