다음 코드에서 I 은 개체를 XML 문자열으로 serialize합니다.XDocument.Parse가이 XML 문자열을 읽도록하려면이 XML 문자열을 어떻게 변경해야합니까?
하지만 XDocument.Parse과 함께하여 XDocument으로이 XML 문자열 을 읽으려고 할 때, 그것은 나에게이 오류 제공 : 루트 수준에서
잘못된 데이터를.
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>
UPDATE : 그것으로 읽을 수 있도록 나는이 XML에해야 할 일을
alt text http://www.deviantsart.com/upload/hhcvmu.png
: 여기은 진수입니다 오류가없는 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 + ")";
}
}
}
답변 :
이 다루는 다른 사람을 위해, 여기에 BOM의 당신의 XML 청소하는 약간의 방법이다, 그래서감사 안드라스,는 GetPreamble()는 그것을 고정 :
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;
}
XML에 문제가 없습니다. 예외를 일으키는 XML이 동일한 것입니까? –
루트 레벨 1 행 1 문자의 데이터가 잘못되었습니다. NotePad ++에 텍스트를 복사하면 첫 번째 문자는 "?"입니다. 그리고 다른 편집기에서 일부 문자 제어 ... –
다음 잘라 내기 및 붙여 넣기 중에 열린 "<"를 잃었습니다. XML에는 아무런 문제가 없습니다. –