C# 在xml中添加没有xml元素的纯文本
我正在尝试生成一个XML文档,该文档如下所示:C# 在xml中添加没有xml元素的纯文本,c#,asp.net,xml,C#,Asp.net,Xml,我正在尝试生成一个XML文档,该文档如下所示: <?xml version="1.0" encoding="UTF-8"?> <Device Version="1.0"> <Packages> <Package Manuf="TI" ModelName="123" MfgPartNumber="CSD86360" Description="DEVICE_INFO" > ".
<?xml version="1.0" encoding="UTF-8"?>
<Device Version="1.0">
<Packages>
<Package Manuf="TI" ModelName="123" MfgPartNumber="CSD86360" Description="DEVICE_INFO" >
".PartialCkt 123 ExtNode = 1 3 4 6
V1 1 3 0
V2 4 6 0
.EndPartialCkt"
</Package>
</Packages>
<Thermal>
Manuf="TI" ModelName="123" MfgPartNumber="CSD86360" Description="DEVICE_INFO"
PowerDissipation="0.1W"
Material="2-Resistor CTM"
Thickness="1mm"
Theta_JB="1.5C/W"
Theta_JC="0C/W"
MaxDieTemperature="100C""
</Thermal>
</Device>
等等
现在我不知道如何包括正常的文本
.PartialCkt 123 ExtNode = 1 3 4 6
V1 1 3 0
V2 4 6 0
.EndPartialCkt
它不是此文档中的XML节点。将其设置为节点的
值
XmlNode PackageNode= xmlDoc.CreateElement("Packages");
XmlNode Package = xmlDoc.CreateElement("Package");
Package.Value = ".PartialCkt 123 ExtNode = 1 3 4 6\nV1 1 3 0\nV2 4 6 0\n.EndPartialCkt";
PackageNode.AppendChild(Package);
使用xml-linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xmlID = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Device Version=\"1.0\"></Device>";
XDocument doc = XDocument.Parse(xmlID);
XElement device = doc.Root;
XElement packages = new XElement("Packages", new object[] {
new XElement("Package", new object[] {
new XAttribute("Manuf","TI"),
new XAttribute("ModelName","123"),
new XAttribute("MfgPartNumber","CSD86360"),
new XAttribute("Description","DEVICE_INFO"),
".PartialCkt 123 ExtNode = 1 3 4 6\n" +
"V1 1 3 0\n" +
"V2 4 6 0 .EndPartialCkt"
})
});
device.Add(packages);
XElement thermal = new XElement("Thermal", new object[] {
new XAttribute("Manuf","TI"),
new XAttribute("ModelName","123"),
new XAttribute("MfgPartNumber","CSD86360"),
new XAttribute("Description","DEVICE_INFO"),
new XAttribute("PowerDissipation","0.1W"),
new XAttribute("Material","2-Resistor CTM"),
new XAttribute("Thickness","1mm"),
new XAttribute("Theta_JB","1.5C/W"),
new XAttribute("Theta_JC","0C/W"),
new XAttribute("MaxDieTemperature","100C")
});
device.Add(thermal);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xmlID=“”;
XDocument doc=XDocument.Parse(xmlID);
XElement device=doc.Root;
XElement packages=新XElement(“packages”,新对象[]{
新XElement(“包”,新对象[]{
新XAttribute(“制造商”、“TI”),
新XAttribute(“ModelName”、“123”),
新XAttribute(“MfgPartNumber”、“CSD86360”),
新XAttribute(“说明”、“设备信息”),
“.PartialCkt 123 ExtNode=1 3 4 6\n”+
“V1 13 0\n”+
“V2 4 6 0.EndPartialCkt”
})
});
设备。添加(包);
XElement thermal=新XElement(“thermal”,新对象[]{
新XAttribute(“制造商”、“TI”),
新XAttribute(“ModelName”、“123”),
新XAttribute(“MfgPartNumber”、“CSD86360”),
新XAttribute(“说明”、“设备信息”),
新XAttribute(“功耗”,“0.1W”),
新XAttribute(“材料”、“双电阻器CTM”),
新的X属性(“厚度”、“1mm”),
新的X属性(“Theta_JB”,“1.5C/W”),
新的X属性(“Theta_JC”,“0C/W”),
新XAttribute(“最高温度”、“100摄氏度”)
});
设备。添加(热);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xmlID = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Device Version=\"1.0\"></Device>";
XDocument doc = XDocument.Parse(xmlID);
XElement device = doc.Root;
XElement packages = new XElement("Packages", new object[] {
new XElement("Package", new object[] {
new XAttribute("Manuf","TI"),
new XAttribute("ModelName","123"),
new XAttribute("MfgPartNumber","CSD86360"),
new XAttribute("Description","DEVICE_INFO"),
".PartialCkt 123 ExtNode = 1 3 4 6\n" +
"V1 1 3 0\n" +
"V2 4 6 0 .EndPartialCkt"
})
});
device.Add(packages);
XElement thermal = new XElement("Thermal", new object[] {
new XAttribute("Manuf","TI"),
new XAttribute("ModelName","123"),
new XAttribute("MfgPartNumber","CSD86360"),
new XAttribute("Description","DEVICE_INFO"),
new XAttribute("PowerDissipation","0.1W"),
new XAttribute("Material","2-Resistor CTM"),
new XAttribute("Thickness","1mm"),
new XAttribute("Theta_JB","1.5C/W"),
new XAttribute("Theta_JC","0C/W"),
new XAttribute("MaxDieTemperature","100C")
});
device.Add(thermal);
}
}
}