有没有人可以帮我解决如何删除XML标签之间的所有空白区域的问题,如下所示:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
要:
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body></note>
在xml节点不应该存在之前,需要xml节点在不同的行但空格
答案 0 :(得分:1)
如果你已经将你的xml加载到一个字符串中,你可以尝试这样做,但我讨厌并且绝对不屑于正则表达式,所以我不能保证这会给你你想要的东西。
apply plugin: 'com.android.application'
apply plugin: 'maven'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.dumainfotech.pratibimb"
minSdkVersion 14
targetSdkVersion 23
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
dexOptions {
javaMaxHeapSize "4g"
preDexLibraries = false
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:23.1.1'
}
我建议使用@Martin Milan的答案。你可以这样做。但是将SaveOptions设置为DisableFormatting以删除所有空格。并且应该给你一行,这应该很容易与你的其他xml文件相媲美。
string x = " <Hello>text </Hello> <itsAme>";
string Replace1 = ">\\s+";
string Replace2 = "\\s+<";
x=Regex.Replace(x, Replace1 , ">");
x=Regex.Replace(x, Replace2, "<");
答案 1 :(得分:1)
我相信System.XML.Linq的XDocument类会为你做这个。 Save和ToString方法都支持SaveOptions参数。将此设置为无应该可以获得您所追求的结果......
答案 2 :(得分:0)
您使用XmlSerializer编写它吗?
使用 XmlWriterSettings 将 String.Empty 指定为 IndentChars ,您有多种选择;例如:
XmlWriterSettings **ws** = new XmlWriterSettings();
ws.CloseOutput = true;
ws.Indent = true;
ws.IndentChars = "";
ws.OmitXmlDeclaration = true;
ws.NewLineHandling = NewLineHandling.Entitize;
ws.NewLineOnAttributes = false;
将其写入文件...
using (TextWriter writer = new StreamWriter(filename, false, Encoding.UTF8))
{
using (XmlWriter wr = XmlWriter.Create(writer, **ws**))
{
// Namespace?
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(wr, o, ns);
}
writer.Close();
}