gpt4 book ai didi

c# - 我必须如何更改此 XML 字符串以便 XDocument.Parse 读取它?

转载 作者:可可西里 更新时间:2023-11-01 09:03:38 25 4
gpt4 key购买 nike

在下面的代码中,我将一个对象序列化为一个 XML 字符串

但是当我尝试使用 XDocument.Parse 将此 XML 字符串读入 XDocument 时,它给了我这个错误:

Invalid data at root level.

XML 是:

<?xml version="1.0" encoding="utf-8"?>
<Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>1</Id>
<FirstName>Jim</FirstName>
<LastName>Jones</LastName>
<ZipCode>23434</ZipCode>
</Customer>

更新:这是十六进制:

![alt text][1] Mod 编辑 - 禁用超链接:指向恶意软件的链接

我必须对该 XML 做什么才能使其无错误地读入 XDocument?

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using System.Xml.Linq;

namespace TestSerialize2342
{
class Program
{
static void Main(string[] args)
{
List<Customer> customers = Customer.GetCustomers();

Console.WriteLine("--- Serializing ------------------");

foreach (var customer in customers)
{
Console.WriteLine("Serializing " + customer.GetFullName() + "...");
string xml = XmlHelpers.SerializeObject<Customer>(customer);

XDocument xdoc = XDocument.Parse(xml);

}

Console.ReadLine();
}

}

public static class StringHelpers
{
public static String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters);
return (constructedString);
}

public static Byte[] StringToUTF8ByteArray(String pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
}

public static class XmlHelpers
{
public static string SerializeObject<T>(object o)
{
MemoryStream ms = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(xtw, o);
ms = (MemoryStream)xtw.BaseStream;
return StringHelpers.UTF8ByteArrayToString(ms.ToArray());
}

public static T DeserializeObject<T>(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
MemoryStream ms = new MemoryStream(StringHelpers.StringToUTF8ByteArray(xml));
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
return (T)xs.Deserialize(ms);
}
}

public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public string Location { get; set; }
public string ZipCode { get; set; }

private int internalValue = 23;

public static List<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Id = 1, FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
customers.Add(new Customer { Id = 2, FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
customers.Add(new Customer { Id = 3, FirstName = "Jack", LastName = "Johnson", ZipCode = "23111" });
customers.Add(new Customer { Id = 4, FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
customers.Add(new Customer { Id = 5, FirstName = "Henry", LastName = "Anderson", ZipCode = "16623" });
return customers;
}

public string GetFullName()
{
return FirstName + " " + LastName + "(" + internalValue + ")";
}

}
}

回答:

感谢 Andras,GetPreamble() 修复了它,所以对于处理此问题的任何其他人,这里有一个清除 BOM 的 XML 的小方法:

public static string RemoveUtf8ByteOrderMark(string xml)
{
string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(byteOrderMarkUtf8))
{
xml = xml.Remove(0, byteOrderMarkUtf8.Length);
}
return xml;
}

最佳答案

因为数据中包含unicode或utf8 BOM marks在流的开头。

您需要跳过流中的任何字节顺序标记 - 您可以从 System.Text.Encoding.GetPreamble() 中识别这些标记方法。

关于c# - 我必须如何更改此 XML 字符串以便 XDocument.Parse 读取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2070661/

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