问题描述
我想从xml中删除 diffgram
和 NewDataSet
标签。
I want to remove the diffgram
and NewDataSet
tag from xml.
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<MACNET diffgr:id="MACNET1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<BATCH_ID>131070</BATCH_ID>
<BATCH_Date_Submitted>12/1/2014 7:36:06 AM</BATCH_Date_Submitted>
<BATCH_Date_Received>12/1/2014 7:36:06 AM</BATCH_Date_Received>
</MACNET>
</NewDataSet>
</diffgr:diffgram>
我已经从数据集
中生成了这个xml XmlSerializer。我使用以下代码。
I have generated this xml from dataset
by XmlSerializer. I have used following code.
//Serialize dataset
using (var memoryStream = new MemoryStream())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(streamWriter, ds, ns);
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
我想删除 ; diffgr:diffgram xmlns:msdata =urn:schemas-microsoft-com:xml-msdataxmlns:diffgr =urn:schemas-microsoft-com:xml-diffgram-v1>
< NewDataSet>
标签从 xml
。并且还要从 MACNET 标签中删除 diffgr:id =MACNET1msdata:rowOrder =0diffgr:hasChanges =inserted
我如何删除这个?
I want to remove <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
and <NewDataSet>
tag from xml
. and also want to remove diffgr:id="MACNET1" msdata:rowOrder="0" diffgr:hasChanges="inserted"
from MACNET tag. How can I remove this?
我想要以下类型的输出
<MACNET>
<BATCH_ID>131070</BATCH_ID>
<BATCH_Date_Submitted>12/1/2014 7:36:06 AM</BATCH_Date_Submitted>
<BATCH_Date_Received>12/1/2014 7:36:06 AM</BATCH_Date_Received>
</MACNET>
推荐答案
首先要做的是序列化 DataSet
通过,或方法,而不是尝试序列化 DataSet
与一个 XmlSerializer
。这将跳过所有
diffgram
废话,并产生以下输出:
The first thing to do is to serialize the DataSet
via the DataSet.GetXml()
, DataSet.WriteXml()
or DataTable.WriteXml()
methods, rather than trying to serialize the DataSet
with an XmlSerializer
. This skips all the diffgram
nonsense and produces the following output:
<NewDataSet>
<MACNET>
<BATCH_ID>131070</BATCH_ID>
<BATCH_Date_Submitted>12/1/2014 7:36:06 AM</BATCH_Date_Submitted>
<BATCH_Date_Received>12/1/2014 7:36:06 AM</BATCH_Date_Received>
</MACNET>
</NewDataSet>
根据您如何创建您的 DataSet
,< NewDataSet>
根节点可能不存在,你完成了。但是如果< NewDataSet>
节点存在,并且您希望生成没有它的XML字符串,则可以使用 RootlessDataSetXmlWriter
建议在这里:,附加的构造函数接受 TextWriter
:
Now, depending on how you created your DataSet
, the <NewDataSet>
root node might not be present, and you are done. But if the <NewDataSet>
node is present, and you want to generate an XML string without it, you can use the RootlessDataSetXmlWriter
suggested here: Save a DataSet ds.WriteXml(…) without <NewDataSet>
Tag?, with an added constructor that accepts a TextWriter
:
public class RootlessDataSetXmlWriter : ElementSkippingXmlWriter
{
private readonly string _dataSetName;
public RootlessDataSetXmlWriter(TextWriter stream, string dataSetName)
: base(stream, (e) => string.Equals(e, dataSetName, StringComparison.OrdinalIgnoreCase))
{
_dataSetName = dataSetName;
this.Formatting = System.Xml.Formatting.Indented;
}
public RootlessDataSetXmlWriter(Stream stream, string dataSetName)
: base(stream, (e) => string.Equals(e, dataSetName, StringComparison.OrdinalIgnoreCase))
{
_dataSetName = dataSetName;
this.Formatting = System.Xml.Formatting.Indented;
}
}
public class ElementSkippingXmlWriter : XmlTextWriter
{
private Predicate<string> _elementFilter;
private int _currentElementDepth;
private Stack<int> _sightedElementDepths;
public ElementSkippingXmlWriter(TextWriter writer, Predicate<string> elementFilter)
: base(writer)
{
_elementFilter = elementFilter;
_sightedElementDepths = new Stack<int>();
}
public ElementSkippingXmlWriter(Stream stream, Predicate<string> elementFilter)
: base(stream, Encoding.UTF8)
{
_elementFilter = elementFilter;
_sightedElementDepths = new Stack<int>();
}
// Rest is as shown in the linked answer.
}
然后像
string xml;
using (var textWriter = new StringWriter())
using (var writer = new RootlessDataSetXmlWriter(textWriter, ds.DataSetName))
{
ds.WriteXml(writer);
xml = textWriter.ToString();
}
这将提供以下输出:
<MACNET>
<BATCH_ID>131070</BATCH_ID>
<BATCH_Date_Submitted>12/1/2014 7:36:06 AM</BATCH_Date_Submitted>
<BATCH_Date_Received>12/1/2014 7:36:06 AM</BATCH_Date_Received>
</MACNET>
这是您需要的。但是,请注意,如果您的根 MACNET
表有多个行,则生成的XML将无效,因为。
which is what you require. Note, however, if your root MACNET
table has more than one row, the XML produced will be invalid, since all XML documents must have one and only one root tag.
这篇关于在c#中从XML中移除diffgram和NewDataSet标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!