gpt4 book ai didi

c# - 如何将字节数组转换为xml?

转载 作者:行者123 更新时间:2023-12-02 00:52:35 26 4
gpt4 key购买 nike

我有一个这样的字节流:

byte[] response =
{
69, 90, 69, 45, 88, 77, 76, 45, 77, 115, 103, 48, 50, 60, 77, 101, 115, 115, 97, 103, 101, 62, 13, 10, 32,
32, 60, 72, 101, 97, 100, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 77, 101, 115, 115, 97, 103, 101, 68,
97, 116, 101, 62, 50, 48, 49, 48, 48, 51, 50, 52, 60, 47, 77, 101, 115, 115, 97, 103, 101, 68, 97, 116,
101, 62, 13, 10, 32, 32, 32, 32, 60, 77, 101, 115, 115, 97, 103, 101, 84, 105, 109, 101, 62, 49, 57, 50,
56, 48, 54, 60, 47, 77, 101, 115, 115, 97, 103, 101, 84, 105, 109, 101, 62, 13, 10, 32, 32, 60, 47, 72,
101, 97, 100, 101, 114, 62, 13, 10, 32, 32, 60, 66, 111, 100, 121, 62, 13, 10, 32, 32, 32, 32, 60, 84,
114, 97, 110, 115, 97, 99, 116, 105, 111, 110, 73, 68, 62, 51, 51, 50, 53, 50, 55, 60, 47, 84, 114, 97,
110, 115, 97, 99, 116, 105, 111, 110, 73, 68, 62, 13, 10, 32, 32, 32, 32, 60, 84, 114, 97, 110, 115, 97,
99, 116, 105, 111, 110, 78, 117, 109, 98, 101, 114, 62, 49, 50, 49, 48, 52, 55, 48, 60, 47, 84, 114, 97,
110, 115, 97, 99, 116, 105, 111, 110, 78, 117, 109, 98, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 80,
104, 111, 110, 101, 78, 117, 109, 98, 101, 114, 62, 54, 51, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57, 60,
47, 80, 104, 111, 110, 101, 78, 117, 109, 98, 101, 114, 62, 13, 10, 32, 32, 32, 32, 60, 65, 109, 111,
117, 110, 116, 62, 48, 48, 48, 48, 48, 48, 50, 53, 48, 48, 60, 47, 65, 109, 111, 117, 110, 116, 62, 13,
10, 32, 32, 32, 32, 60, 82, 101, 115, 117, 108, 116, 62, 48, 51, 60, 47, 82, 101, 115, 117, 108, 116, 62,
13, 10, 32, 32, 60, 47, 66, 111, 100, 121, 62, 13, 10, 60, 47, 77, 101, 115, 115, 97, 103, 101, 62
};

我想从中提取 xml。尝试了以下操作:

XmlDocument doc2 = new XmlDocument();
MemoryStream ms = new MemoryStream(response);
doc2.Load(ms);

但是有一个异常(exception),比如:

System.Xml.dll 中发生类型为“System.Xml.XmlException”的未处理异常

附加信息:根级别的数据无效。第 1 行,位置 1。

我对这个 xml 东西真的很陌生,doc2.load() 方法应该做什么?它会创建我以后可以阅读的任何 xml 文件吗,它只是一个内存集合吗?

最佳答案

问题是您的 byte [] 数组表示一个字符串,该字符串的开头有一些非 XML 字符。如果我这样做

string s;
using (var ms = new MemoryStream(response))
using (var reader = new StreamReader(ms))
{
s = reader.ReadToEnd();

Console.WriteLine(s);
}

我明白了

EZE-XML-Msg02<Message>
<Header>
<MessageDate>20100324</MessageDate>
<MessageTime>192806</MessageTime>
</Header>
<Body>
<TransactionID>332527</TransactionID>
<TransactionNumber>1210470</TransactionNumber>
<PhoneNumber>639999999999</PhoneNumber>
<Amount>0000002500</Amount>
<Result>03</Result>
</Body>
</Message>

必须先删除 EZE-XML-Msg02。执行此操作的最佳方法是首先不要将其存储在 XML 中。但是,如果您无法以某种方式阻止它被包含在 XML 中,您可以这样做:

XmlDocument doc2 = new XmlDocument();
using (var ms = new MemoryStream(response))
using (var reader = new StreamReader(ms))
{
while (!reader.EndOfStream && reader.Peek() > -1 && (char)reader.Peek() != '<')
reader.Read();
if (!reader.EndOfStream)
doc2.Load(reader);
}

关于c# - 如何将字节数组转换为xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38572080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com