gpt4 book ai didi

c# - 如何在 .net 中读取具有不同层次结构的 xml 文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:07:54 39 4
gpt4 key购买 nike

我有 7GB 的大 xml 文件,我在这里粘贴了一小部分

       <?xml version="1.0" encoding="UTF-8"?>
<bulkFile xmlns:un="utranNrm.xsd"
xmlns:es="Attributes.15.25.xsd"
xmlns:xn="generic.xsd" xmlns:gn="geran.xsd" xmlns="configData.xsd">
<fileHeader fileFormatVersion="32.615 V4.5" Name="Wmart"/>
<configData dnPrefix="Undefined">
<xn:SubNetwork id="M_ROOT">
<xn:VsDataContainer id="1">
<xn:attributes>
<xn:vsDataType>vsDataAreas</xn:vsDataType>
<xn:vsDataFormatVersion>SpecificAttributes</xn:vsDataFormatVersion>
<es:vsDataAreas/>
</xn:attributes>
<xn:VsDataContainer id="424_2_2">
<xn:attributes>
<xn:vsDataType>vsDataPlmn</xn:vsDataType>
<xn:vsDataFormatVersion>SpecificAttributes</xn:vsDataFormatVersion>
<es:vsDataPlmn>
<es:userLabel></es:userLabel>
<es:mcc>424</es:mcc>
<es:mnc>2</es:mnc>
<es:mncLength>2</es:mncLength>
<es:aliasPlmnIdentities>
<es:mcc>424</es:mcc>
<es:mnc>2</es:mnc>
<es:mncLength>2</es:mncLength>
</es:aliasPlmnIdentities>
</es:vsDataPlmn>
</xn:attributes>
<xn:VsDataContainer id="10">
<xn:attributes>
<xn:vsDataType>vsDataLocationArea</xn:vsDataType>
<xn:vsDataFormatVersion>SpecificAttributes</xn:vsDataFormatVersion>
<es:vsDataLocationArea>
<es:userLabel></es:userLabel>
<es:lac>10</es:lac>
<es:t3212>10</es:t3212>
<es:att>1</es:att>
</es:vsDataLocationArea>
</xn:attributes>
</xn:VsDataContainer>
</xn:VsDataContainer>
</xn:VsDataContainer>
<xn:SubNetwork id="G-Mum">
<xn:attributes>
<xn:userDefinedNetworkType>SiteSolutions</xn:userDefinedNetworkType>
<xn:userLabel>G-Mum</xn:userLabel>
</xn:attributes>
<xn:MeContext id="32509_I_East">
<xn:VsDataContainer id="23502_I_AAN_UAEU_Crescent_East">
<xn:attributes>
<xn:vsDataType>vsDataMeContext</xn:vsDataType>
<xn:vsDataFormatVersion>EricssonSpecificAttributes.15.25</xn:vsDataFormatVersion>
<es:vsDataMeContext>
<es:userLabel>23502_I_AAN_UAEU_Crescent_East</es:userLabel>
<es:ipAddress>10.235.20.10</es:ipAddress>
<es:neMIMversion>vF.1.108</es:neMIMversion>
<es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
<es:bcrLastChange>1474785017888</es:bcrLastChange>
<es:bctLastChange>1470699087457</es:bctLastChange>
<es:multiStandardRbs6k>false</es:multiStandardRbs6k>
<es:mixedModeRadio>false</es:mixedModeRadio>
<es:mirrorMIBversion>F.1.100.S.1.6</es:mirrorMIBversion>
<es:stnNodes></es:stnNodes>
</es:vsDataMeContext>
</xn:attributes>
</xn:VsDataContainer>
<xn:ManagedElement id="1">
<xn:attributes>
</xn:attributes>
<xn:VsDataContainer id="1">
<xn:attributes>
<xn:vsDataType>vsDataManagedElement</xn:vsDataType>
<xn:vsDataFormatVersion>Attributes.15.25.xsd</xn:vsDataFormatVersion>
<es:vsDataManagedElement>
<es:siteRef>SubNetwork=M_ROOT,Site=32509_I_East</es:siteRef>
<es:productType>Node</es:productType>
<es:productName>RBS6601L</es:productName>
<es:productNumber></es:productNumber>
<es:productRevision></es:productRevision>
<es:prodDesignation>0</es:prodDesignation>
</es:vsDataManagedElement>
</xn:attributes>
</xn:VsDataContainer>
.
.
<xn:VsDataContainer id="1">
<xn:attributes>
<xn:vsDataType>vsDataENodeBFunction</xn:vsDataType>
<xn:vsDataFormatVersion>Attributes.15.25.xsd</xn:vsDataFormatVersion>
<es:vsDataENodeBFunction>
<es:userLabel></es:userLabel>
<es:dscpLabel>24</es:dscpLabel>
<es:dnsLookupOnTai>1</es:dnsLookupOnTai>
<es:eNBId>32509</es:eNBId>
</es:vsDataENodeBFunction>
</xn:attributes>
<xn:VsDataContainer id="BC_3250_32509_L1_B">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:earfcndl>1651</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>
..
<xn:VsDataContainer id="BD_4250_32509_L1_B">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:earfcndl>1651</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>

<xn:VsDataContainer id="CC_3250_32509_L1_C">
<xn:attributes>
<xn:vsDataType>testranCellFDD</xn:vsDataType>
<es:earfcndl>165</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>
</xn:VsDataContainer>
..

</xn:ManagedElement>
</xn:MeContext>
<xn:MeContext id="32509_I_west">
<xn:VsDataContainer id="32509_I_AAN_UAEU_Crescent_East">
<xn:attributes>
<xn:vsDataType>vsDataMeContext</xn:vsDataType>
<xn:vsDataFormatVersion>Attributes.15.25</xn:vsDataFormatVersion>
<es:vsDataMeContext>
<es:userLabel>32509_I_AAN_UAEU_Crescent_East</es:userLabel>
<es:ipAddress>10.235.20.10</es:ipAddress>
<es:neMIMversion>vF.1.108</es:neMIMversion>
<es:lostSynchronisation>SYNCHRONISED</es:lostSynchronisation>
<es:bcrLastChange>1474785017888</es:bcrLastChange>
<es:bctLastChange>1470699087457</es:bctLastChange>
<es:multiStandardRbs6k>false</es:multiStandardRbs6k>
<es:mixedModeRadio>false</es:mixedModeRadio>
<es:mirrorMIBversion>F.1.100.S.1.6</es:mirrorMIBversion>
<es:stnNodes></es:stnNodes>
</es:vsDataMeContext>
</xn:attributes>
</xn:VsDataContainer>
<xn:ManagedElement id="1">
<xn:attributes>
</xn:attributes>
<xn:VsDataContainer id="1">
<xn:attributes>
<xn:vsDataType>vsDataManagedElement</xn:vsDataType>
<xn:vsDataFormatVersion>Attributes.15.25.xsd</xn:vsDataFormatVersion>
<es:vsDataManagedElement>
<es:siteRef>SubNetwork=M_ROOT,Site=32509_I_East</es:siteRef>
<es:productType>Node</es:productType>
<es:productName>RBS6601L</es:productName>
<es:productNumber></es:productNumber>
<es:productRevision></es:productRevision>
<es:prodDesignation>0</es:prodDesignation>
</es:vsDataManagedElement>
</xn:attributes>
</xn:VsDataContainer>
.
.
<xn:VsDataContainer id="1">
<xn:attributes>
<xn:vsDataType>vsDataENodeBFunction</xn:vsDataType>
<xn:vsDataFormatVersion>Attributes.15.25.xsd</xn:vsDataFormatVersion>
<es:vsDataENodeBFunction>
<es:userLabel></es:userLabel>
<es:dscpLabel>24</es:dscpLabel>
<es:dnsLookupOnTai>1</es:dnsLookupOnTai>
<es:eNBId>32509</es:eNBId>
</es:vsDataENodeBFunction>
</xn:attributes>
<xn:VsDataContainer id="CC_3250_32509_L1_B">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:earfcndl>1651</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>
..
<xn:VsDataContainer id="CD_4250_32509_L1_B">
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
<es:earfcndl>1651</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>

<xn:VsDataContainer id="DC_3250_32509_L1_C">
<xn:attributes>
<xn:vsDataType>testranCellFDD</xn:vsDataType>
<es:earfcndl>165</es:earfcndl>
<es:earfcnul>19651</es:earfcnul>
</xn:attributes>
</xn:VsDataContainer>
</xn:VsDataContainer>
..

</xn:ManagedElement>
</xn:MeContext>
</xn:SubNetwork>

</xn:SubNetwork>
</configData>
</bulkFile>

在这里,我想获取我声明的 C# 变量的值,类似于 subnetwork , sitename , eNBId , cellname , earfcndlearfcnul 所有信息都位于根标签 <xn:SubNetwork id="M_ROOT"> 下标记,rest all 用于根目录下的所有站点,这里我只粘贴一个站点详细信息。逻辑是

   subnetwork = <xn:SubNetwork id > attribute value
sitename = <xn:MeContext id> attribute value
eNBId = <es:eNBId> value, here the important thing is, there will be huge number of `<xn:VsDataContainer id="1">` parent tag,but the filtration is based on `<xn:vsDataType>` inner tag which is of `vsDataENodeBFunction`.
cellname = `<xn:VsDataContainer id>` attribute value and
`earfcndl` and `earfcnul` is values for `<es:earfcndl> and <es:earfcnul>` tag .Here also filtration is based on <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>

所以我想得到subnetwork = G-Mum,,sitename = 32509_I_East,,eNBId=32509,,cellname=BC_3250_32509_L1_B,,earfcndl=1651,,earfcnul=19651 .

我在这里做了以下代码,但卡住了,不知道该怎么做。但我可以保证这是我们可以读取 7Gb Xml 文件的唯一方法(xmlreader(XElement)XNode.ReadFrom 方法),其余的将抛出“内存不足异常”

using (XmlReader xr = XmlReader.Create(path))
{
xr.MoveToContent();
XNamespace un = xr.LookupNamespace("un");
while (xr.Read())
{
while (xr.NodeType == XmlNodeType.Element && xr.NamespaceURI == un && xr.LocalName == "M_ROOT")
{
XElement pin = (XElement)XNode.ReadFrom(xr);
var data = from atts in pin.Elements(un + "attributes")
select new
{
eNBId= (string)atts.Element(es + "eNBId"),

}
}
}

编辑层次结构只有一个 xn:SubNetwork id="M_ROOT"标签,其中包含所有区域,例如再次包含所有站点信息的下标签,再次包含所有类型为 vsDataEUtranCellFDD 的单元格信息

编辑@jdweng 感谢您的帮助,现在他们又增加了 xml,现在是我们使用 (XElement)XElement.ReadFrom(reader); 获取的代码片段 xml。本身抛出内存不足异常。如何删除不必要的 xml 部分。我的层次结构是

<xn:SubNetwork id="M_ROOT">--------------Root
<xn:SubNetwork id="G-Mum">---------Region
<xn:MeContext id="32509_I_East">-----sites
<xn:VsDataContainer id="BC_3250_32509_L1_B"> ---Cells
<xn:attributes>
<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>
</xn:MeContext>
</xn:SubNetwork>
</xn:SubNetwork>

所以我在乞求我的问题时发布的是必要的数据,其余所有不必要的

编辑@jdweng,是的,我发布的内容是必需的,我正在再次编辑我的问题。层次结构是

`<xn:SubNetwork>`          --------Root  and only one 

然后经过几行 <xn:SubNetwork>包含区域会成组出现,但这里只有id以G-开头有效所以在这里 G-Mum那么<xn:MeContext>包含站点将以组的形式出现,其中又包含很多 <xn:VsDataContainer> , 但这里有一个包含子节点 <xn:vsDataType>vsDataManagedElement</xn:vsDataType>是有效的一个为了得到<es:eNBId> .然后再经过几行 <xn:VsDataContainer>子节点 <xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType> contains cells will come.This(cells) 是最里面的一个。 所以会有大量的<xn:VsDataContainer>会出来,只有子节点<xn:vsDataType>vsDataENodeBFunction</xn:vsDataType>获得 Enbid<xn:vsDataType>vsDataEUtranCellFDD</xn:vsDataType>对于细胞是进口的

编辑分析大文件后,我开始知道单元格在站点下,但我将其发布在外面,即 vsDataEUtranCellFDD 位于标签 vsDataENodeBFunction 下。我编辑了我发布的 xml

编辑 2016 年 10 月 10 日xml变化不大,多了一个VsDataContainer以前有没有VsDataContainer<xn:vsDataType>vsDataENodeBFunction</xn:vsDataType> ,因此 vsDataENodeBFunction没抓到。我用了Skip(1)但没用 vsDataManagedElement

最佳答案

您有一个非常大的文件,因此最好使用 XmlReader。试试这段代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication16
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
enum State
{
GET_SUBNETWORK,
GET_MECONTEXT,
GET_CONTAINERS,
GET_SUBNETWORK_OR_MECONTEXT
}
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true };
XmlReader reader = XmlReader.Create(FILENAME, settings);

reader.ReadToFollowing("SubNetwork", "generic.xsd");
Network.network.name = reader.GetAttribute("id");
Network.network.subnetworks = new List<SubNetwork>();

string xnNameSpace = reader.LookupNamespace("xn");
reader.ReadToFollowing("SubNetwork", xnNameSpace);

Sites newSite = null;
SubNetwork subNetWork = null;
Boolean endElement = false;

State state = State.GET_SUBNETWORK;
while (!reader.EOF)
{
switch (state)
{
case State.GET_SUBNETWORK:
if (reader.Name != "xn:SubNetwork")
{
reader.ReadToFollowing("SubNetwork", xnNameSpace);
}
if (!reader.EOF)
{
subNetWork = new SubNetwork();
Network.network.subnetworks.Add(subNetWork);
subNetWork.name = reader.GetAttribute("id");
subNetWork.sites = new List<Sites>();

state = State.GET_MECONTEXT;
}
break;

case State.GET_MECONTEXT :
if (reader.Name != "xn:MeContext")
{
if (reader.NodeType == XmlNodeType.EndElement)
{
endElement = true;
state = State.GET_SUBNETWORK;
}
else
{
endElement = false;
reader.ReadToFollowing("MeContext", xnNameSpace);
}
}
if (!reader.EOF && !endElement)
{
state = State.GET_CONTAINERS;
newSite = new Sites();
subNetWork.sites.Add(newSite);
newSite.sitename = reader.GetAttribute("id");
reader.ReadToFollowing("ManagedElement", xnNameSpace);
reader.ReadToFollowing("VsDataContainer", xnNameSpace);
}
break;

case State.GET_CONTAINERS:

if (!reader.EOF)
{
switch (reader.NodeType)
{
case XmlNodeType.EndElement:
state = State.GET_SUBNETWORK_OR_MECONTEXT;
break;
case XmlNodeType.Text:
string text = reader.ReadContentAsString();
break;

case XmlNodeType.Element:

string elementName = reader.Name;
XElement element = (XElement)XElement.ReadFrom(reader);
if (elementName == "xn:VsDataContainer")
{
XElement vsDataENodeBFunction = element.Descendants().Where(a => a.Name.LocalName == "vsDataENodeBFunction").FirstOrDefault();
if (vsDataENodeBFunction != null)
{
XElement eNBId = element.Descendants().Where(a => a.Name.LocalName == "eNBId").FirstOrDefault();
if (eNBId != null)
{
newSite.eNBId = (int)eNBId;
}
}
List<XElement> vsDataContainers = element.Descendants().Where(a => a.Name.LocalName == "VsDataContainer").ToList();
List<XElement> vsDataEUtranCellFDD = vsDataContainers.Where(x => x.Descendants().Where(a => (a.Name.LocalName == "vsDataType") && ((string)a == "vsDataEUtranCellFDD")).Any()).ToList();
if (vsDataEUtranCellFDD.Any())
{
List<CellName> cells = vsDataEUtranCellFDD.Select(x => new CellName()
{
id = (string)x.Attribute("id"),
earfcndl = (int)x.Descendants().Where(a => a.Name.LocalName == "earfcndl").FirstOrDefault(),
earfcnul = (int)x.Descendants().Where(a => a.Name.LocalName == "earfcnul").FirstOrDefault()
}).ToList();
if (newSite.cellName == null)
{
newSite.cellName = new List<CellName>();
}
newSite.cellName.AddRange(cells);
}
}
break;

default:
Console.WriteLine("Unexpected Results");
string elementText = reader.ReadContentAsString();
Console.ReadLine();
break;
}
}
break;

case State.GET_SUBNETWORK_OR_MECONTEXT :
switch(reader.NodeType)
{
case XmlNodeType.Element :
switch(reader.Name)
{
case "xn:MeContext" :
state = State.GET_MECONTEXT;
break;
case "xn:SubNetwork" :
state = State.GET_SUBNETWORK;
break;
default :
reader.ReadStartElement();
break;
}
break;

case XmlNodeType.Text:
string text = reader.ReadContentAsString();
break;

case XmlNodeType.EndElement:
reader.ReadEndElement();
break;

default:
Console.WriteLine("Unexpected Results");
string elementText = reader.ReadContentAsString();
Console.ReadLine();
break;

}
break;
}
}
}

}
public class Network
{
public static Network network = new Network();
public string name { get; set; }
public List<SubNetwork> subnetworks { get; set; }
}
public class SubNetwork
{
public string name { get; set; }
public List<Sites> sites { get; set; }

}
public class Sites
{
public string sitename { get; set; }
public int eNBId { get; set; }
public List<CellName> cellName { get; set; }
}
public class CellName
{
public string id { get; set; }
public int earfcndl { get; set; }
public int earfcnul { get; set; }
}
}

关于c# - 如何在 .net 中读取具有不同层次结构的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805526/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com