用XDocument读取CDATA吗? [英] read CDATA with XDocument?
问题描述
要读取此代码段中的CDATA文本需要进行哪些更改
XDocument connectedPCsXML = XDocument.Load(new StreamReader(Registry.GetValue(@``HKEY_LOCAL_MACHINE \ software \ myCompany \ xml'',"path" ;,@"C:\ xml \")+"ConnectedPCs.xml"));;
名称= PC.Element(名称").Value,
IP = PC.Element("IP")"值",
,DnsName = PC.Element("DnsName").Value
};
< xml版本="1.0" encoding ="utf-8''?
< ConnectedPCs>
< ConnectedPC>
<名称> PC 1</Name>< DnsName> Pc1DnsName</DnsName>
< IP> 127.1.0.1</IP>
< Description 5100#1</Description>
<![CDATA [
Multi line
text 1
]]>
</ConnectedPC>
< ConnectedPC>
< DnsName> Pc2DnsName</DnsName<>
; IP> 127.1.0.2</IP>
< Description> Dell 5100#2</Description>
<![CDATA [
文本2
]] >
</ConnectedPC>
</ConnectedPCs>
Hi, what changes are needed to read the CDATA text in this code fragment
XDocument connectedPCsXML = XDocument.Load(new StreamReader(Registry.GetValue(@"HKEY_LOCAL_MACHINE\software\myCompany\xml", "path",@"C:\xml\") + "ConnectedPCs.xml"));
var PCs = from PC in connectedPCsXML.Descendants("ConnectedPC")
select new
{
Name = PC.Element("Name").Value,
IP = PC.Element("IP").Value,
Description = PC.Element("Description").Value,
DnsName = PC.Element("DnsName").Value
};
<?xml version="1.0" encoding="utf-8"?>
<ConnectedPCs>
<ConnectedPC>
<Name>PC 1</Name>
<DnsName>Pc1DnsName</DnsName>
<IP>127.1.0.1</IP>
<Description>Dell 5100 #1</Description>
<![CDATA[
Multi line
text 1
]]>
</ConnectedPC>
<ConnectedPC>
<Name>PC 2</Name>
<DnsName>Pc2DnsName</DnsName>
<IP>127.1.0.2</IP>
<Description>Dell 5100 #2</Description>
<![CDATA[
Multi line
text 2
]]>
</ConnectedPC>
</ConnectedPCs>
推荐答案
如果有单个CDATA数据段,则最后一个,即在我的示例中为例,然后使用LastNode有效
XDocument connectedPCsXML = XDocument.Load(new StreamReader(Registry.GetValue(@"HKEY_LOCAL_MACHINE \ software \ jdda \ jcl","path",@"; C:\ jcl \'')+"ConnectedPCs.xml"));
var PC =来自connectedPCsXML中的PC.Descendants("ConnectedPC") = PC.Element(名称").Value,
.IP = PC.Element("IP").Value,
. DnsName = PC.Element("DnsName").Value,
CDataText =((System.Xml.Linq.XCData)(PC.LastNode)).Value
一种更好的方式来读取不具有2种限制的CDATA部分?
If there is a single CDATA data section and it is last, which is the case in my sample, then using LastNode works
XDocument connectedPCsXML = XDocument.Load(new StreamReader(Registry.GetValue(@"HKEY_LOCAL_MACHINE\software\jdda\jcl", "path",@"C:\jcl\") + "ConnectedPCs.xml"));
var PCs = from PC in connectedPCsXML.Descendants("ConnectedPC")
select new
{
Name = PC.Element("Name").Value,
IP = PC.Element("IP").Value,
Description = PC.Element("Description").Value,
DnsName = PC.Element("DnsName").Value,
CDataText =((System.Xml.Linq.XCData)(PC.LastNode)).Value
};
Is there a better way ot read CDATA section(s) that doesn't have the 2 restictions?
这篇关于用XDocument读取CDATA吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!