我正在尝试生成一个如下所示的 XML 文档:
<?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>
我正在尝试以下方式:
XmlDocument xmlDoc = new XmlDocument();
XmlNode DeviceNode= xmlDoc.CreateElement("Device");
XmlNode PackageNode= xmlDoc.CreateElement("Packages");
PackageNode.AppendChild(Package);
ResistorsNode.AppendChild(ResistorNode);
XmlAttribute Attr1= xmlDoc.CreateAttribute("ModelName");
Attr1.Value = "123";
XmlAttribute Attr2 = xmlDoc.CreateAttribute("MfgPartNumber");
Attr2.Value = "CSD86360";
等等..
现在我不确定如何包含普通文本
.PartialCkt 123 ExtNode = 1 3 4 6
V1 1 3 0
V2 4 6 0
.EndPartialCkt
这不是本文档中的 XML 节点。
最佳答案
使用 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);
}
}
}
关于c# - 在 XML 中添加没有 xml 元素的纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44581522/