我有一个file.xml用Iso-latin-15(又名Iso-Latin-9)编码的XML文档
<?xml version="1.0" encoding="iso-8859-15"?>
<root xmlns="http://stackoverflow.com/demo">
<f>€.txt</f>
</root>
Run Code Online (Sandbox Code Playgroud)
从我最喜欢的文本编辑器,我可以告诉这个文件在Iso-Latin-15中正确编码(它不是UTF-8).
我的软件是用C#编写的,想要提取元素f.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");
Run Code Online (Sandbox Code Playgroud)
在现实生活中,我有一个XMLResolver来设置凭据.但基本上,我的代码就是这么简单.装载进展顺利,我没有任何例外.
现在,我提取值时的问题:
//xnsm is the XmlNameSpace manager
XmlNode n = xmlDoc.SelectSingleNode("//root/f", xnsm);
if (n != null)
String filename = n.InnerText;
Run Code Online (Sandbox Code Playgroud)
Visual Studio调试器显示filename = ?.txt
它可能只是一个Visual Studio错误.不幸的是File.Exists(filename)返回false,而文件实际存在.
怎么了?
我有这样的XML文件(某些应用程序的输出):
<data>
<call function="get_user_data">
<output>
<integer name="my_id" value="-31" />
<string name="login" value="root" />
<integer name="ip_list" value="2" />
<array name="i">
<item>
<ip_address name="user_ip" value="0.0.0.0" />
<ip_address name="user_mask" value="0.0.0.0"/>
</item>
<item>
<ip_address name="user_ip" value="94.230.160.230" />
<ip_address name="user_mask" value="255.255.255.0"/>
</item>
</array>
<integer name="modules" value="2" />
<array name="i">
<item>
<string name="module_name" value="core" />
</item>
<item>
<string name="module_name" value="devices" />
</item>
</array>
<integer name="addition_modules" value="0"/>
<array name="i"/>
</output>
</call>
</data>
Run Code Online (Sandbox Code Playgroud)
我需要为这个perl结构解析它:
$data = {
my_id => -31,
login => "root",
ip_list => [
{
user_ip …Run Code Online (Sandbox Code Playgroud) 反序列化XML时遇到问题:
System.InvalidOperationException was unhandled
Message=There is an error in XML document (0, 0).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at xsdToObject.Program.Main(String[] args) in D:\Old Documents\Projects\xsdToObject\xsdToObject\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Xml.XmlException
Message=Root element is missing.
Source=System.Xml
LineNumber=0
LinePosition=0
SourceUri=""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception …Run Code Online (Sandbox Code Playgroud) 我正在尝试将XML反序列化为具有许多相同类型元素的C#对象.为了清楚起见,我已经减少了内容.我的C#类看起来像这样:
[XmlInclude(typeof(RootElement))]
[XmlInclude(typeof(Entry))]
[Serializable, XmlRoot("Form")]
public class DeserializedClass
{
public List<Entry> listEntry;
public RootElement rootElement { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我定义Entry和RootElement类,如下所示:
public class RootElement
{
public string rootElementValue1 { get; set; }
public string rootElementValue2 { get; set; }
}
public class Entry
{
public string entryValue1 { get; set; }
public string entryValue2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图反序列化的XML结构如下所示:
<Entry property="value">
<entryValue1>Data 1</entryValue1>
<entryValue2>Data 2</entryValue2>
<RootElement>
<rootElementValue1>Data 3</rootElementValue1>
<rootElementValue2>Data 4</rootElementValue2>
</RootElement>
<RootElement>
<rootElementValue1>Data 5</rootElementValue1>
<rootElementValue2>Data 6</rootElementValue2>
</RootElement>
</Entry>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将要将多个RootElement元素反序列化到C#对象的List中.要反序列化我使用以下内容: …
假设我将此XML作为字符串:
<calles>
<calle>
<nombre>CALLAO AV.</nombre>
<altura>1500</altura>
<longitud>-58.3918617027</longitud>
<latitud>-34.5916734896</latitud>
<barrio>Recoleta</barrio>
</calle>
</calles>
Run Code Online (Sandbox Code Playgroud)
并且创建了我创建的Type I来映射XML:
public class Ubicacion
{
public string Latitud { get; set; }
public string Longitud { get; set; }
public string Nombre { get; set; }
public string Altura { get; set; }
public string Barrio { get; set; }
public Ubicacion() { }
}
Run Code Online (Sandbox Code Playgroud)
我需要获取该XML文件并使用这些值创建一个对象...
有人知道快速的方法吗?用C#?我一直在尝试这个,但根本不工作......
XElement dir = XElement.Parse(text);
Ubicacion informacion = from d in dir.Elements("calle").
select new Ubicacion
{
Longitud = d.Element("longitud").Value,
Latitud = d.Element("latitud").Value, …Run Code Online (Sandbox Code Playgroud) 我想在SQL中保存一个ac #.net类的实例,以便以后检索.我可以使用LINQ to SQL来添加一个完整的记录,其中包含构成该类的所有xml.
现在我如何检索该xml并重建类对象实例?
我是AS的新手,我假设有一种方法可以做到这一点,我只是没有搞清楚.基本上,我试图使用一个返回xml并返回一个Object的服务,而不管xml的结构如何.在.Net中我使用XmlSerializer.Deserialize类...在AS中是否有等价物?
我能够找到SimpleXMLDecoder,但我似乎无法让它工作 - 它看起来也可能只适用于节点?无论哪种方式,那里的例子很稀疏而且很难遵循,我只想知道如何把xml这样:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Company>
<Id>2</Id>
<Name>Stan</Name>
<Size>10</Size>
</Company>;
Run Code Online (Sandbox Code Playgroud)
简单地把它变成一个对象 - 这可能不用编写我自己的解析器吗?谢谢.
我在 XML 文件中有一些数据,正在通过流读取。我可以访问流对象,使用它我必须反序列化数据并填充列表。我的模型类如下所示
[XmlRoot("invoices")]
public class Invoice
{
[XmlElement(ElementName = "description", IsNullable= true)]
public string Description { get; set; }
[XmlElement(ElementName = "invoice_date")]
public string InvoiceDate { get; set; }
[XmlElement(ElementName = "invoice_number")]
public string InvoiceNumber { get; set; }
[XmlElement(ElementName = "date_due")]
public string DateDue { get; set; }
[XmlElement(ElementName = "amount")]
public string Amount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
下面是我用来读取数据的方法的片段
public List<Invoice> ParseRecords(Stream stream)
{
List<Invoice> invoices = new List<Invoice>();
try
{
stream.Position = 0;
XmlSerializer serializer = …Run Code Online (Sandbox Code Playgroud)