我正在尝试从XML文档中获取一些值。
这是从DevExpress
<?xml version="1.0" encoding="ISO-8859-1"?>
<XtraSerializer version="15.2.5.0">
<Items>
<Item1 SelectedStencils="" CanvasSizeMode="AutoSize" PageSize="40000,40000"
Theme="Office" ItemKind="DiagramRoot" Size="40000,40000">
<Children>
<Item1 ItemKind="DiagramShape" Size="100,75" Shape="BasicShapes.Rectangle"
Position="19500,19440"/>
<Item2 ItemKind="DiagramShape" Size="100,90" Shape="BasicShapes.Triangle"
Position="19540,19660"/>
<Item3 ItemKind="DiagramConnector" EndPoint="19590,19660"
BeginPoint="19550,19515" EndItem="1" BeginItem="0" Points="19550,19650
19590,19650" EndArrow="Filled90"/>
<Item4 ItemKind="DiagramShape" Size="100,75" Shape="BasicShapes.Rectangle"
Position="19940,19440"/>
<Item5 ItemKind="DiagramConnector" EndPoint="19940,19477.5"
BeginPoint="19600,19477.5" EndItem="3" BeginItem="0" Points=""
EndArrow="Filled90"/>
<Item6 ItemKind="DiagramConnector" EndPoint="19940,19477.5"
BeginPoint="19590,19660" EndItem="3" BeginItem="1" Points="19590,19525
19930,19525 19930,19478" EndArrow="Filled90"/>
</Children>
</Item1>
</Items>
</XtraSerializer>
我试图从“ DiagramConnector”部分获取值“ BeginItem”和“ EndItem”。
我试图通过DevExpress API BeginItem获取这些值– EndItem
但是每次尝试时,即使保存数据时值始终为Null,但在XML文档中却不是null。
所以我认为直接从XML文档获取值将是最好/唯一的方法。
这是我试图从xml文档中获取值的方法。
请记住,可能有上百万个DiagramConnection对象。
class Program
{
static readonly string FolderLocation = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Diagram Samples";
static readonly string FileName = "d.xml";
static void Main(string[] args)
{
string FullPath = System.IO.Path.Combine(FolderLocation, FileName);
using (XmlDocumentCreator xmlDocument = new XmlDocumentCreator(FullPath))
{
string data = xmlDocument.ReadDocument.ToString();
if (data.Contains("BeginItem") && data.Contains("EndItem"))
{
string xpath = "Items/Item1SelectedStencils/Children";
var nodes = xmlDocument.XMLDocument.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
Console.Write(childrenNode.SelectSingleNode("//BeginItem").Value);
}
}
}
Console.ReadKey();
}
}
这是我创建的XML Document类,
我知道它不适用于该项目,因为SelectNodes需要XmlDocument,仅用于测试。
class XmlDocumentCreator : IDisposable
{
public XmlDocument XMLDocument { get; set; }
// <summary>
/// XmlDocument Creator
/// </summary>
/// <param name="xmlDocumentPath">xmlPath</param>
public XmlDocumentCreator(string xmlDocumentPath)
{
XMLDocument = new XmlDocument();
XMLDocument.Load(xmlDocumentPath);
}
/// <summary>
/// Returns An Xml Document
/// Which Can Then Be Read.
/// </summary>
/// <returns></returns>
public XElement ReadDocument
{
get
{
if (XMLDocument != null)
{
return XElement.Parse(XMLDocument.InnerXml);
}
throw new Exception("XML Document Object Was Not Created");
}
}
/// <summary>
/// Will Dispose The XML Document
/// And Also Call The Garbage Collector.
/// </summary>
public void Dispose()
{
if (XMLDocument != null)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.SuppressFinalize(this);
GC.Collect();
Console.WriteLine("XML Document Object Disposed");
}
}
}
在一个理想的世界中,一旦我能够获得循环中每个值所需要的值,就可以将它们放到anonymous type上并对其进行任何处理。