C# 如何删除<;xml版本=";1.0“&燃气轮机;C中的元素#
变量中的我的xml内容如下所示:C# 如何删除<;xml版本=";1.0“&燃气轮机;C中的元素#,c#,.net,xml,xml-parsing,C#,.net,Xml,Xml Parsing,变量中的我的xml内容如下所示: var xml = DownloadString(@"http://192.168.1.50:8983/solr/core-live/select?q=*%3A*&wt=xslt&tr=custom.xsl"); <?xml version="1.0" encoding="UTF-8"?> <xml version="1.0"> <item> <sku>12944</sku> <
var xml = DownloadString(@"http://192.168.1.50:8983/solr/core-live/select?q=*%3A*&wt=xslt&tr=custom.xsl");
<?xml version="1.0" encoding="UTF-8"?>
<xml version="1.0">
<item>
<sku>12944</sku>
<title>test</title</item>
</xml>
DownloadString是一个函数/方法
public static string DownloadString(string address)
{
string text;
using (var client = new WebClient())
{
text = client.DownloadString(address);
}
return text;
}
在调试xml变量和xml o/p时,如下所示:
var xml = DownloadString(@"http://192.168.1.50:8983/solr/core-live/select?q=*%3A*&wt=xslt&tr=custom.xsl");
<?xml version="1.0" encoding="UTF-8"?>
<xml version="1.0">
<item>
<sku>12944</sku>
<title>test</title</item>
</xml>
问候,,
Jatinstringfilepath=“C:\\file.xml”;
List strList=File.ReadAllLines(filePath.ToList();
StringBuilder sb=新的StringBuilder();
int ctr=0;
foreach(strList中的字符串str)
{
ctr++;
如果(ctr==1 | | ctr==strList.Count)
继续;
某人附加(str);
}
stringfilepath=“C:\\file.xml”;
List strList=File.ReadAllLines(filePath.ToList();
StringBuilder sb=新的StringBuilder();
int ctr=0;
foreach(strList中的字符串str)
{
ctr++;
如果(ctr==1 | | ctr==strList.Count)
继续;
某人附加(str);
}
也许您需要在字符串中使用replace方法
text = text.Replace("<xml version=\"1.0\">", "");
text = text.Replace("</xml>", "");
text=text.Replace(“,”);
text=text.Replace(“,”);
也许您需要在字符串中使用replace方法
text = text.Replace("<xml version=\"1.0\">", "");
text = text.Replace("</xml>", "");
text=text.Replace(“,”);
text=text.Replace(“,”);
I在DownloadString()方法上同时使用string replace()函数
我尝试了这个代码,它工作得很好
public static string DownloadString(string address)
{
string text;
using (var client = new WebClient())
{
text = client.DownloadString(address);
}
return text.Replace("<xml version=\"1.0\">", "").Replace("</xml>", "");
}
公共静态字符串下载字符串(字符串地址)
{
字符串文本;
使用(var client=new WebClient())
{
text=client.DownloadString(地址);
}
返回文本。替换(“,”)。替换(“,”);
}
I在DownloadString()方法上同时使用string replace()函数
我尝试了这个代码,它工作得很好
public static string DownloadString(string address)
{
string text;
using (var client = new WebClient())
{
text = client.DownloadString(address);
}
return text.Replace("<xml version=\"1.0\">", "").Replace("</xml>", "");
}
公共静态字符串下载字符串(字符串地址)
{
字符串文本;
使用(var client=new WebClient())
{
text=client.DownloadString(地址);
}
返回文本。替换(“,”)。替换(“,”);
}
在我的例子中,除了@user1040975对该问题的解决方案外,我还必须将XmlWriterSettings的OmitXmlDeclaration属性设置为true,这样不带我创建的编码的新声明就会出现,最后代码如下所示:
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = new UTF8Encoding(false),
OmitXmlDeclaration = true
};
using (XmlWriter xmlWriter = XmlWriter.Create(convertedPath, settings))
{
XDocument xDoc = XDocument.Parse(innerXml);
xDoc.Declaration = new XDeclaration("1.0",null,null);
xDoc.Save(xmlWriter);
}
在我的例子中,除了@user1040975对该问题的解决方案外,我还必须将XmlWriterSettings的OmitXmlDeclaration属性设置为true,因此将出现没有我创建的编码的新声明,最后代码如下所示:
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = new UTF8Encoding(false),
OmitXmlDeclaration = true
};
using (XmlWriter xmlWriter = XmlWriter.Create(convertedPath, settings))
{
XDocument xDoc = XDocument.Parse(innerXml);
xDoc.Declaration = new XDeclaration("1.0",null,null);
xDoc.Save(xmlWriter);
}
但那就不是XML了?你想删除XML标记吗?@DrSchizo否不删除第一个标记我想删除第二个标记。但这样就不是XML了?要删除XML标记吗?@DrSchizo否不删除第一个标记我要删除第二个标记。