有没有办法在PowerShell中利用.NET的XmlSerializer类的功能?
更具体地说,能够轻松地将.NET类型/序列化为Xml,例如
XmlSerializer s = new XmlSerializer(typeof(MyType));
TextReader r = new StreamReader("sample.xml");
MyType myType = (MyType)s.Deserialize(r);
r.Close();
Run Code Online (Sandbox Code Playgroud)
我知道我可以从PowerShell上面调用,但有没有办法避免在单独的程序集中定义MyType?谢谢
[编辑]由于似乎无法从PowerShell添加类似.NET的类型,请允许我重新定位我的问题:是否有一种简单的方法,如XmlSerializer,在PowerShell中序列化类型?我必须从PS读取/写入一些.xml,并且宁愿在手动执行之前利用这些功能.
我一直在尝试使用WCF连接到某些Web服务,但是当我尝试调用我需要的函数时,我一直收到错误.
这是我得到的错误:
System.InvalidOperationException : XmlSerializer attribute System.Xml.Serialization.XmlChoiceIdentifierAttribute is not valid in Item. Only XmlElement, XmlArray, XmlArrayItem, XmlAnyAttribute and XmlAnyElement attributes are supported when IsWrapped is true.
错误发生在它甚至调用实际服务之前,它甚至不会因为我试图调用的方法而发生.问题在于在WCF生成的类中定义的另一个方法.
我已经能够将问题跟踪到XSD中用于定义WSDL的代码段:
<xs:choice minOccurs="0">
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType"/>
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType"/>
</xs:choice>
Run Code Online (Sandbox Code Playgroud)
相应的生成代码:
[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.1")]
[System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http:integration.sprint.com/interfaces/manageSubscriberServices/v1/manageSubscr" +
"iberServices.xsd", IncludeInSchema=false)]
public enum ItemChoiceType2
{
additionalSocInd,
skipServiceValidationInd,
}
Run Code Online (Sandbox Code Playgroud)
当我注释掉上面的枚举及其所有引用时,该服务有效.XSD中还有其他xs:choice语句似乎没有引起任何问题.
更新:进一步调查显示,当您有以下内容时:
元素直接在序列元素内定义:
<xs:sequence>
<xs:element ... />
...
<xs:choice minOccurs="0">
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType" />
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType" />
</xs:choice>
...
<xs:element ... />
</xs:sequence>
Run Code Online (Sandbox Code Playgroud)
由svcutil生成的代理会导致上述错误.
当更改为如下所示:
<xs:sequence>
<xs:element …Run Code Online (Sandbox Code Playgroud) 我想将类序列化为XML,为其分配XML属性.片段:
[XmlType(TypeName = "classmy")]
public class MyClass2 : List<object>
{
[XmlAttribute(AttributeName = "myattr")]
public string Name { get; set; }
}
public class MyConst
{
public MyConst()
{
MyClass2 myClass2 = new MyClass2 { 10, "abc" };
myClass2.Name = "nomm";
XmlSerializer serializer = new XmlSerializer(typeof(MyClass2));
serializer.Serialize(Console.Out, myClass2);
}
}
Run Code Online (Sandbox Code Playgroud)
但是生成的XML看起来像这样
<?xml version="1.0" encoding="IBM437"?>
<classmy xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="xsd:int">10</anyType>
<anyType xsi:type="xsd:string">abc</anyType>
</classmy>
Run Code Online (Sandbox Code Playgroud)
一切都很好,唯一的例外是myClass2.Name没有被序列化.我期待着一些东西
<classmy myattr="nomm" [...]>[...]</classmy>
Run Code Online (Sandbox Code Playgroud)
...为什么不是序列化的,它怎么可能?
由于我是Android的新手,因此我正在构建一个应用程序,需要在该应用程序中更新Geo服务器上的图层数据。对于这种情况,我将与XML正文请求一起调用post方法。
我需要在请求正文中以xml格式发送数据,并且需要在xml中获取响应。为此,我尝试使用XmlSerializer代替JsonFeature,但出现错误,提示“ XmlSerializer不是同伴对象,需要在此处初始化”
val httpClient = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(Json.nonstrict)
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!!
我有一个网站,只要它到达我的代码中的以下位置就会抛出OutOfMemoryExceptions:
XmlSerializer xs = new XmlSerializer(t, xoverrides);
Run Code Online (Sandbox Code Playgroud)
看到这只发生在Web服务器上时,我没有大量关于为什么会发生这种情况的信息.我知道它正在序列化的对象并不是太严肃 - 绝对不到每个MB.
你之前有这个吗?觉得帮我诊断一下这个问题?任何帮助表示赞赏.
谢谢!
问题:我在http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx上使用可序列化的字典类 来序列化字典.哪个工作正常,但我遇到了一个恼人的问题.
<System.Xml.Serialization.XmlRoot("DataBase")> _
Public Class cDataBase
<System.Xml.Serialization.XmlNamespaceDeclarations()> _
Public ns As New System.Xml.Serialization.XmlSerializerNamespaces()
<System.Xml.Serialization.XmlElement("Tables")> _
Public Tables1 As New SerializableDictionary(Of String, cTable)
End Class ' cDataBase
Run Code Online (Sandbox Code Playgroud)
当我序列化上述类的实例时,创建的xml如下所示:
<Tables>
<item>
<key>
<string>MyTable</string>
</key>
<value>
<Table CreationDate="0001-01-01T00:00:00" LastModified="0001-01-01T00:00:00">
<Columns>
<Column Name="PrimaryKeyName">
<DataType>Uniqueidentifier</DataType>
<Length>36</Length>
</Column>
</Columns>
<Rows>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
Run Code Online (Sandbox Code Playgroud)
如果我能弄清楚如何将键从<string>重命名为属性中定义的内容,那将会很好
<key>
<string>MyTable</string>
</key>
Run Code Online (Sandbox Code Playgroud)
基本上类似于XmlArrayItem属性,如下面,if(仅)字典是一个数组...
<System.Xml.Serialization.XmlArray("Tables")> _
<System.Xml.Serialization.XmlArrayItem("Table")> _
Public Tables As New List(Of cTable)
Run Code Online (Sandbox Code Playgroud)
我想尝试将字符串更改为从字符串继承的自定义类,我可以配备一个名称,但问题是,不能从字符串继承...
从Windows事件查看器我可以得到以下xml结构:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="XXXXXXXXXX" Guid="{YYYYYYYY}" />
<EventID>XYZ</EventID>
<Version>0</Version>
<Level>L</Level>
<Task>A</Task>
<Opcode>0</Opcode>
<Keywords>0x000xyzh</Keywords>
<TimeCreated SystemTime="2012-06-28T15:44:04.997837000Z" />
<EventRecordID>153</EventRecordID>
<Correlation ActivityID="{DDDDDDDDD}" />
<Execution ProcessID="199999" ThreadID="90990" />
<Channel>Microsoft-Windows-ABCDEFG/Admin</Channel>
<Computer>myPC</Computer>
<Security UserID="ABCABC" />
</System>
<EventData>
<Data Name="name1">data1</Data>
<Data Name="name2">data2</Data>
<Data Name="name3">data3</Data>
</EventData>
<RenderingInfo Culture="en-US">
<Message>some message </Message>
<Level>Information</Level>
<Task>XYZ</Task>
<Opcode>Info</Opcode>
<Channel />
<Provider />
<Keywords>
<Keyword>XYZ</Keyword>
</Keywords>
</RenderingInfo>
</Event>
Run Code Online (Sandbox Code Playgroud)
我只对xml的EventData部分感兴趣.我创建了以下非常简单的类:
public class Event
{
public EventData EventData;
}
public class EventData
{
public String[] Data;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用以下代码:
XmlSerializer serializer = new XmlSerializer(typeof(Event));
StringReader …Run Code Online (Sandbox Code Playgroud) 调用
List<PC> _PCList = new List<PC>();
...add Pc to PCList..
WriteXML<List<PC>>(_PCList, "ss.xml");
Run Code Online (Sandbox Code Playgroud)
功能
public static void WriteXML<T>(T o, string filename)
{
string filePath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Genweb2\\ADSnopper\\" + filename;
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("TheRootElementName"));
ser.Serialize(writer, o); // error
}
File.WriteAllText(filePath,xmlDoc.InnerXml);
}
Run Code Online (Sandbox Code Playgroud)
内在的例外
无法转换类型为'System.Collections.Generic.List
1[PC]' to type 'System.Collections.Generic.List1 [System.Collections.Generic.List`1 [PC]]'的对象.
请帮忙
我试图序列化一个类,将XML文件写为多个片段,即将该类的每个对象作为单个片段写入,而不使用XML头/根.以下是示例代码:
[Serializable]
public class Test
{
public int X { get; set; }
public String Y { get; set; }
public String[] Z { get; set; }
public Test()
{
}
public Test(int x, String y, String[] z)
{
X = x;
Y = y;
Z = z;
}
}
class Program
{
static void Main(string[] args)
{
Test t1 = new Test(1, "t1", new[] { "a", "b" });
Test t2 = new Test(2, "t2", new[] { "c", "d", "e" …Run Code Online (Sandbox Code Playgroud) 一些上下文:我们有一个自定义XSD,并使用WSCF.blue生成WSDL和C#代码.客户端使用ChannelFactory<T>并共享接口,该接口包括WSCF.blue添加的所有属性以匹配XSD中的内容.
我正在尝试实现IErrorHandler.ProvideFault它提供泛型的地方FaultException<T>,但在客户端我得到的是非泛型的FaultContract.这是我的ProvideFault方法的样子:
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (!(error is FaultException))
{
FaultException faultException = FaultExceptionFactory.CreateFaultException(error);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
}
}
Run Code Online (Sandbox Code Playgroud)
在每个服务方法中,如果我使用try/catch,throw FaultExceptionFactory.CreateFaultException(ex)它按预期工作,所以我认为[FaultContract],工厂,绑定等都是正确的.以防万一,这就是工厂的工作原理:
BusinessRuleFaultExceptionType businessRuleFaultException = new BusinessRuleFaultExceptionType();
BusinessRuleFaultException.Code = exception.Code.ToString();
BusinessRuleFaultException.Reason = exception.Message;
return new FaultException<BusinessRuleFaultExceptionType>(
businessRuleFaultException,
exception.Message,
new FaultCode(exception.Code.ToString())
);
Run Code Online (Sandbox Code Playgroud)
我认为问题在于如何创建消息IErrorHandler,也许是在CreateMessageFault().我已经读过这个动作应该faultException.Action代替null,但实际上faultException.Action是null.也许这导致了这个问题.我可以在工厂设置一个动作,但是动作应该是什么,为什么不会出现手动抛出? …