C# 如何使用c从xml元素中删除xmlns标记#
我想删除这个xmlns=”http://www.AB.com/BC/QualityDeviationCase/1_0“来自除根标记以外的所有xml元素。使用C#的xml元素。我已经编写了下面的代码,以获取xml格式。但无法从xml元素中删除xmlns标记C# 如何使用c从xml元素中删除xmlns标记#,c#,xml,xsd,C#,Xml,Xsd,我想删除这个xmlns=”http://www.AB.com/BC/QualityDeviationCase/1_0“来自除根标记以外的所有xml元素。使用C#的xml元素。我已经编写了下面的代码,以获取xml格式。但无法从xml元素中删除xmlns标记 <?xml version="1.0" encoding="utf-16"?> <QualityDeviationCaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins
<?xml version="1.0" encoding="utf-16"?>
<QualityDeviationCaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CaseID xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">2</CaseID>
<Description xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">Air filter is not present</Description>
<StartDate xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">0001-01-01T00:00:00</StartDate>
<LastUpdated xmlns="http://www.AB.com/BC/QualityDeviationCase/1_0">0001-01-01T00:00:00</LastUpdated>
</QualityDeviationCaseType>
根据我上面的代码,我的结果是每个元素都带有xml标记。我想使用c#从元素标记中删除
xmlns
属性在XML中具有重要的语义:它将所有元素放在默认名称空间http://www.AB.com/BC/QualityDeviationCase/1_0
然而,我对C#并不熟悉:去掉它意味着在语义层面而不是句法层面上进行推理
具体地说,应该删除要导出的XML文档中所有元素的名称空间,或者不首先将它们放在任何名称空间中。如果C#library是完整的,它应该提供一种方法来实现这一点。
xmlns
属性在XML中具有重要的语义:它将所有元素放在默认名称空间http://www.AB.com/BC/QualityDeviationCase/1_0
然而,我对C#并不熟悉:去掉它意味着在语义层面而不是句法层面上进行推理
具体地说,应该删除要导出的XML文档中所有元素的名称空间,或者不首先将它们放在任何名称空间中。如果C#library已完成,它应该提供一种方法来完成此操作。您尝试过正则表达式吗
using System.Text.RegularExpressions;
...
string pattern = @"xmlns=""[a-zA-Z0-9:\/._]{1,}""";
using (StringWriter stringwriter = new System.IO.StringWriter())
{
XmlSerializer ser = new XmlSerializer(typeof(QualityDeviationCaseType));
ser.Serialize(stringwriter, oQualityDeviationCaseType);
string s = stringwriter.ToString();
Match m = Regex.Match(s,pattern);
if(m.Success)
s=s.Replace(m.Value, "");
sampleChannel.Publish(s);
//This line of code sending my xml file to IBM WMQ.
}
我对XmlSerializer不太熟悉,但我认为这应该是可行的。Match将只找到以“xmlns=”开头并以“.”结尾的内容。您尝试过正则表达式吗
using System.Text.RegularExpressions;
...
string pattern = @"xmlns=""[a-zA-Z0-9:\/._]{1,}""";
using (StringWriter stringwriter = new System.IO.StringWriter())
{
XmlSerializer ser = new XmlSerializer(typeof(QualityDeviationCaseType));
ser.Serialize(stringwriter, oQualityDeviationCaseType);
string s = stringwriter.ToString();
Match m = Regex.Match(s,pattern);
if(m.Success)
s=s.Replace(m.Value, "");
sampleChannel.Publish(s);
//This line of code sending my xml file to IBM WMQ.
}
我对XmlSerializer不太熟悉,但我认为这应该是可行的。Match将只查找以“xmlns=”开头并以“结尾”结尾的内容
using System.Text.RegularExpressions;
...
string pattern = @"xmlns=""[a-zA-Z0-9:\/._]{1,}""";
using (StringWriter stringwriter = new System.IO.StringWriter())
{
XmlSerializer ser = new XmlSerializer(typeof(QualityDeviationCaseType));
ser.Serialize(stringwriter, oQualityDeviationCaseType);
string s = stringwriter.ToString();
Match m = Regex.Match(s,pattern);
if(m.Success)
s=s.Replace(m.Value, "");
sampleChannel.Publish(s);
//This line of code sending my xml file to IBM WMQ.
}