C# 通过用XML元素替换占位符将字符串转换为XML
亲爱的各位委员: 我在C#工作。我有以下字符串:C# 通过用XML元素替换占位符将字符串转换为XML,c#,xml,parsing,xml-parsing,C#,Xml,Parsing,Xml Parsing,亲爱的各位委员: 我在C#工作。我有以下字符串: "This {1} is {2}really{3} great {4}, isn't it?" 其中,花括号之间的占位符可能对应于以下标记(包含在数组中): 案例1: {1} = <cf underline="single"> {2} = <cf bold="True"> {3} = </cf> {4} = </cf> <seg> This <cf underline="
"This {1} is {2}really{3} great {4}, isn't it?"
其中,花括号之间的占位符可能对应于以下标记(包含在数组中):
案例1:
{1} = <cf underline="single">
{2} = <cf bold="True">
{3} = </cf>
{4} = </cf>
<seg>
This
<cf underline="single" startID=1 endId=4>
is
<cf bold="True" startID=2 endId=3>
really
</cf>
great
</cf>
, isn't it?"
</seg>
{1}=
{2} =
{3} =
{4} =
或
案例2:
{1}=
{2} =
{3} =
{4} =
i、 e.相同级别的节点或具有子节点的节点
我想在我的段中插入标记,这可以通过在我的数组中循环并用指定的值替换相应的占位符来轻松完成。这是最简单的部分。
但是,我想在opening元素中插入一个属性,该属性将包含opening和closing标记的ID,因此我的XML如下所示:
using System;
using System.Xml;
public class Program
{
public static void WalkXml(XmlElement node, ref int index) {
node.SetAttribute("startIndex", index.ToString());
foreach(XmlNode child in node.ChildNodes) {
XmlElement element = (child as XmlElement);
if (element != null) {
index++;
WalkXml(element, ref index);
}
}
node.SetAttribute("endIndex", (++index).ToString());
}
public static void WriteXml(string message, string[] tags) {
var xmlStr = $"<seg>{String.Format(message, tags)}</seg>";
var xmlObj = new XmlDocument();
xmlObj.LoadXml(xmlStr);
int index = 1;
WalkXml(xmlObj.DocumentElement, ref index);
Console.WriteLine(xmlObj.InnerXml);
}
public static void Main()
{
string[] tags1 = new string[] {
"<cf underline=\"single\">",
"<cf bold=\"True\">",
"</cf>",
"</cf>"
};
string[] tags2 = new string[] {
"<cf underline=\"single\">",
"</cf>",
"<cf bold=\"True\">",
"</cf>"
};
string message = "This {0} is {1}really{2} great {3}, isn't it?";
WriteXml(message, tags1);
WriteXml(message, tags2);
}
}
案例1:
{1} = <cf underline="single">
{2} = <cf bold="True">
{3} = </cf>
{4} = </cf>
<seg>
This
<cf underline="single" startID=1 endId=4>
is
<cf bold="True" startID=2 endId=3>
really
</cf>
great
</cf>
, isn't it?"
</seg>
这
是
真正地
伟大的
“是吗?”
案例2:
<seg>
This
<cf underline="single" startID=1 endId=2>
is
</cf>
really
<cf bold="True" startID=3 endId=4>
great
</cf>
, isn't it?"
</seg>
这
是
真正地
伟大的
“是吗?”
有没有人能告诉我如何做到这一点?我一直在寻找解决这个问题的方法,但我甚至不知道从哪里开始
我提前感谢大家的支持
问候,
Laurent通常不会尝试使用字符串处理Xml。这是一个头痛的问题,你会把它搞砸很多次,直到你把它做好。使用XmlDocument 对于模板替换,可以使用字符串。然后,我建议将其转换为xml文档,然后遍历它来设置属性 大概是这样的:
using System;
using System.Xml;
public class Program
{
public static void WalkXml(XmlElement node, ref int index) {
node.SetAttribute("startIndex", index.ToString());
foreach(XmlNode child in node.ChildNodes) {
XmlElement element = (child as XmlElement);
if (element != null) {
index++;
WalkXml(element, ref index);
}
}
node.SetAttribute("endIndex", (++index).ToString());
}
public static void WriteXml(string message, string[] tags) {
var xmlStr = $"<seg>{String.Format(message, tags)}</seg>";
var xmlObj = new XmlDocument();
xmlObj.LoadXml(xmlStr);
int index = 1;
WalkXml(xmlObj.DocumentElement, ref index);
Console.WriteLine(xmlObj.InnerXml);
}
public static void Main()
{
string[] tags1 = new string[] {
"<cf underline=\"single\">",
"<cf bold=\"True\">",
"</cf>",
"</cf>"
};
string[] tags2 = new string[] {
"<cf underline=\"single\">",
"</cf>",
"<cf bold=\"True\">",
"</cf>"
};
string message = "This {0} is {1}really{2} great {3}, isn't it?";
WriteXml(message, tags1);
WriteXml(message, tags2);
}
}
使用系统;
使用System.Xml;
公共课程
{
公共静态XML(XmlElement节点,ref int索引){
SetAttribute(“startIndex”,index.ToString());
foreach(node.ChildNodes中的XmlNode子节点){
XmlElement=(子元素为XmlElement);
if(元素!=null){
索引++;
WalkXml(元素,参考索引);
}
}
SetAttribute(“endIndex”,(++index.ToString());
}
公共静态void WriteXml(字符串消息,字符串[]标记){
var xmlStr=$“{String.Format(message,tags)}”;
var xmlObj=新的XmlDocument();
LoadXml(xmlStr);
int指数=1;
WalkXml(xmlObj.DocumentElement,参考索引);
WriteLine(xmlObj.InnerXml);
}
公共静态void Main()
{
字符串[]标记1=新字符串[]{
"",
"",
"",
""
};
字符串[]标记2=新字符串[]{
"",
"",
"",
""
};
string message=“这{0}是{1}真的{2}很棒{3},不是吗?”;
WriteXml(消息,标记1);
WriteXml(消息,标记2);
}
}
这是输出
<seg startIndex="1" endIndex="6">This <cf underline="single" startIndex="2" endIndex="5"> is <cf bold="True" startIndex="3" endIndex="4">really</cf> great </cf>, isn't it?</seg>
<seg startIndex="1" endIndex="6">This <cf underline="single" startIndex="2" endIndex="3"> is </cf>really<cf bold="True" startIndex="4" endIndex="5"> great </cf>, isn't it?</seg>
这真是太棒了,不是吗?
这真是太棒了,不是吗?
演示代码也将索引放在“seg”上,但它显示了这一想法,您应该能够相应地进行调整。通常不要尝试使用字符串处理Xml。这是一个头痛的问题,你会把它搞砸很多次,直到你把它做好。使用XmlDocument 对于模板替换,可以使用字符串。然后,我建议将其转换为xml文档,然后遍历它来设置属性 大概是这样的:
using System;
using System.Xml;
public class Program
{
public static void WalkXml(XmlElement node, ref int index) {
node.SetAttribute("startIndex", index.ToString());
foreach(XmlNode child in node.ChildNodes) {
XmlElement element = (child as XmlElement);
if (element != null) {
index++;
WalkXml(element, ref index);
}
}
node.SetAttribute("endIndex", (++index).ToString());
}
public static void WriteXml(string message, string[] tags) {
var xmlStr = $"<seg>{String.Format(message, tags)}</seg>";
var xmlObj = new XmlDocument();
xmlObj.LoadXml(xmlStr);
int index = 1;
WalkXml(xmlObj.DocumentElement, ref index);
Console.WriteLine(xmlObj.InnerXml);
}
public static void Main()
{
string[] tags1 = new string[] {
"<cf underline=\"single\">",
"<cf bold=\"True\">",
"</cf>",
"</cf>"
};
string[] tags2 = new string[] {
"<cf underline=\"single\">",
"</cf>",
"<cf bold=\"True\">",
"</cf>"
};
string message = "This {0} is {1}really{2} great {3}, isn't it?";
WriteXml(message, tags1);
WriteXml(message, tags2);
}
}
使用系统;
使用System.Xml;
公共课程
{
公共静态XML(XmlElement节点,ref int索引){
SetAttribute(“startIndex”,index.ToString());
foreach(node.ChildNodes中的XmlNode子节点){
XmlElement=(子元素为XmlElement);
if(元素!=null){
索引++;
WalkXml(元素,参考索引);
}
}
SetAttribute(“endIndex”,(++index.ToString());
}
公共静态void WriteXml(字符串消息,字符串[]标记){
var xmlStr=$“{String.Format(message,tags)}”;
var xmlObj=新的XmlDocument();
LoadXml(xmlStr);
int指数=1;
WalkXml(xmlObj.DocumentElement,参考索引);
WriteLine(xmlObj.InnerXml);
}
公共静态void Main()
{
字符串[]标记1=新字符串[]{
"",
"",
"",
""
};
字符串[]标记2=新字符串[]{
"",
"",
"",
""
};
string message=“这{0}是{1}真的{2}很棒{3},不是吗?”;
WriteXml(消息,标记1);
WriteXml(消息,标记2);
}
}
这是输出
<seg startIndex="1" endIndex="6">This <cf underline="single" startIndex="2" endIndex="5"> is <cf bold="True" startIndex="3" endIndex="4">really</cf> great </cf>, isn't it?</seg>
<seg startIndex="1" endIndex="6">This <cf underline="single" startIndex="2" endIndex="3"> is </cf>really<cf bold="True" startIndex="4" endIndex="5"> great </cf>, isn't it?</seg>
这真是太棒了,不是吗?
这真是太棒了,不是吗?
演示代码也将索引放在“seg”上,但它显示了这个想法,您应该能够相应地进行调整。尝试下面的代码。你做的事情确实有点倒退。您正在字符串之间添加xml,而不是将字符串添加到xml中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication43
{
class Program
{
static void Main(string[] args)
{
string[] input = { "This", "is","really", "great",", isn't it?"};
KeyValuePair<int, int>[] startEndId = {
new KeyValuePair<int,int>(1,4),
new KeyValuePair<int,int>(2,3)
};
var seq = new XElement("seg", new object[] {
input[0],
new XElement("cf", new object[] {
new XAttribute("underline", "single"),
new XAttribute("startID", startEndId[0].Key),
new XAttribute("endID", startEndId[0].Value),
input[1],
new XElement("cf", new object[] {
new XAttribute("bold", "True"),
new XAttribute("startID", startEndId[1].Key),
new XAttribute("endID", startEndId[1].Value),
input[2],
}),
input[3],
}),
input[4]
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序43
{
班级计划
{
静态void Main(字符串[]参数)
{
string[]input={“This”、“is”、“really”、“great”、“,不是吗?”};
KeyValuePair[]startEndId={
新的KeyValuePair(1,4),
新的KeyValuePair(2,3)
};
var seq=新的元素(“seg”,新对象[]{
输入[0],
新元素(“cf”,新对象[]{
新XAttribute(“下划线”、“单个”),
新的XAttribute(“startID”,startEndId[0].Key),
新的XAttribute(“endID”,startEndId[0]。值),
输入[1],
新元素(“cf”,新对象[]{
新XAttribute(“粗体”、“真实”),
新XAttribute(“startID”,startEnd