我有通过
解析的xml<ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate">
<Test>
<CurrencyCode>SEKAUDPMI</CurrencyCode>
<CurrencyType>AUD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>6.5656</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKBRLPMI</CurrencyCode>
<CurrencyType>BRL</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>2.4376</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKCADPMI</CurrencyCode>
<CurrencyType>CAD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>7.0771</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKCHFPMI</CurrencyCode>
<CurrencyType>CHF</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>951.7346</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
</ns0:Root>
该xml具有(“ Test”)子节点,这些子节点经常出现。
我需要做的实际上是删除(" <ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate"> ")
这样XML输出是
<Test>
<CurrencyCode>SEKAUDPMI</CurrencyCode>
<CurrencyType>AUD</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>6.5656</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
<CurrencyCode>SEKBRLPMI</CurrencyCode>
<CurrencyType>BRL</CurrencyType>
<CurrencyDate>2019 Juni</CurrencyDate>
<CurrencyRate>2.4376</CurrencyRate>
<RecordCreated>2019-06-30</RecordCreated>
<RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
我试图做的是
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());
System.Xml.XmlNode xNode = xDoc.FirstChild;
System.Xml.XmlNode xParent = xNode.ParentNode;
System.Xml.XmlNodeList xChilds = xNode.ChildNodes;
xDoc.RemoveChild(xNode);
foreach (System.Xml.XmlNode node in xChilds)
{
xParent.AppendChild(node);
}
但是主要的问题是返回的xml仅包含一个孩子(“测试”),而没有其他孩子。
我还尝试使用Linq在帖子中找到它的地方
XDocument input = XDocument.Load("input.xml");
XElement firstChild = input.Root.Elements().First();
但这会返回相同的内容,对我的情况没有帮助。
有人可以在正确的方向帮助我吗?
谢谢。
答案 0 :(得分:1)
如果您真的只需要文本字符串,最快的方法是在Root
XElement上使用XmlReader:
XDocument xDoc = XDocument.Parse(bizobj.Message.BodyPart.GetOriginalDataStream());
XElement root = xDoc.Root;
XmlReader reader = root.CreateReader();
reader.MoveToContent();
string txt = reader.ReadInnerXml();
答案 1 :(得分:0)
如果有人想看看并使用它,只需将我的最终代码放在这里。 无论如何,菲尔伯特还是把我扔向正确方向的那个人。
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());
string output = xDoc.DocumentElement.InnerXml;
byte[] byteArray = Encoding.ASCII.GetBytes(output);
MemoryStream stream = new MemoryStream(byteArray);
stream.Position = 0;
bizobj.Message.BodyPart.Data = stream;
return bizobj;