XML中的快速替换节点名称

时间:2016-12-13 18:06:01

标签: c# xml linq-to-xml xmldocument fetchxml

我使用管理抓取CRM的旧服务:crmService.Fetch(fetchXml)

我得到 XML字符串结果如下:

<resultset>
<result>
<new_categoria name="Cliente" formattedvalue="1">1</new_categoria>
<new_name>Admin</new_name>
<new_tipodecampanaid>{F8F29978-4E0F-AE92-FB43-48B4DC406B1F}</new_tipodecampanaid>
<statuscode name="Activo">0</statuscode>
</result>

<result>
<new_categoria name="Client" formattedvalue="1">1</new_categoria>
<new_name>Client</new_name>
<new_tipodecampanaid>{758341BA-4661-D694-6743-8D2DC875793E}</new_tipodecampanaid>
<statuscode name="Activo">0</statuscode>
</result>

<result>

</resultset>

我们需要(因为别名不支持获取方法)替换多个 节点名称

1 - 将 new_categoria 节点名称替换为 org_category

2 - 将 new_name 节点名称替换为 org_name

3 - 将 new_tipodecampanaid 节点名称替换为 org_campaignid

我们需要高绩效,结果可能很大。

使用XmlDocument

var doc = new XmlDocument();
doc.LoadXml(fetchXMLResult);

XmlNode root = doc.SelectSingleNode("resultset");
foreach (XmlNode childNode in root.ChildNodes)
{

}

使用XDocument

XDocument resultset = XDocument.Parse(fetchXMLResult);
if (resultset.Root == null || !resultset.Root.Elements("result").Any())
{
    return;
}
resultset.Root.Elements("result")

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

为了提高速度,您可以使用XmlReader运行该文件,并使用XmlWriter将您读取的每个节点写入新文件。

有关示例,请参阅this link

答案 1 :(得分:1)

当您拥有大型xml文件时,请始终使用XmlReader。试试这段代码

P_out = 0:100;   
Po= 130 ;         
dP= 4.7 ;         
N_TRX=6;
P_max= 20;
p_sleep= 75;

if 0< P_out <= P_max
    P = N_TRX*Po+N_TRX*dP*P_out;
else 
   P=N_TRX* p_sleep;
end
%%%%%
Po= 130 ;          
dP= 2.8 ;         
N_TRX=6;
P_max= 40;

if 0< P_out <= P_max
    P1= N_TRX*Po+N_TRX*dP*P_out;
else 
   P1=N_TRX* p_sleep;
end
%%%%%
Po= 130 ;          
dP= 5.9 ;         
N_TRX=2;
P_max= 20;

if 0< P_out <= P_max
    P2= N_TRX*(Po+dP*P_out);
else 
   P_in2=N_TRX* p_sleep;
end
%%%%%
Po= 110 ;          
dP= 4.2;         
N_TRX=6;
P_max= 20;

if 0< P_out <= P_max_macro
    P3= N_TRX*Po+N_TRX*dP*P_out;
else 
   P3=N_TRX* p_sleep;
end

figure, plot(P_out,P,'b'); hold on, plot(P_out,P1,'g'); hold on,      plot(P_out,P2,'r'); hold on,plot (P_out,P3,'y'),hold off