我无法正确添加命名空间信息 [英] I am failing to add namespace information properly
问题描述
我正在尝试序列化以下代码:
[Serializable]
public class Part
{
[XmlElementAttribute( AA)]
public string a;
[XmlElementAttribute( BB)]
public int b;
public Part()
{
a = 部分字符串; b = 99 ;
}
}
[可序列化]
public class Container
{
[XmlElementAttribute( STRING)]
public string str;
[XmlElementAttribute( PART)]
public 部分;
public Container()
{
str = some str; part = new Part();
}
}
class 计划
{
静态 void Main( string [] args)
{
Container container = new Container();
TextWriter write = new StreamWriter( MyNewClass .XML跨度>);
XmlWriterSettings settings = new XmlWriterSettings();
settings.IndentChars = \t;
settings.Indent = true ;
XmlWriter xmlWriter = XmlWriter.Create(write,settings);
XmlSerializer ser = new XmlSerializer(container.GetType());
ser.Serialize(xmlWriter,container);
xmlWriter.Close(); write.Close();
}
}
我得到的输出如下:
<? xml version = 1.0 encoding = utf-8 ?>
< 容器 xmlns:xsi = http:// www.w3.org/2001/XMLSchema-instance\" xmlns:xsd = http://www.w3.org/2001/XMLSchema >
< STRING > some str < / STRING >
< PART > ;
< AA > 部分字符串< / AA >
< BB > 99 < / BB >
< / PART >
< / Containe r >
我试图以多种方式添加名称空间等信息。但我没有成功。我希望序列化后的输出如下所示:
<? xml version = 1.0 encoding = utf-8 ?>
< 容器 xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation = http ://www.omg.org/spec/ReqIF/20110401/reqif.xsd reqif.xsd http://www.omg.org/spec/ReqIF/20110401/reqif-xhtml reqif-xhtml.xsd xmlns:reqif-xh tml = http://www.w3.org/1999/xhtml < span class =code-attribute> xmlns:xhtml = http://www.w3.org/1999/xhtml xmlns:门 = http://www.ibm.com/rdm/doors/REQIF/xmlns/1.0 xmlns:rm-reqif = http://www.ibm.com/rm/reqif xmlns:rm = http://www.ibm.com/rm xmlns = http:// www .omg.org / spec / ReqIF / 20110401 / reqif.xsd xmlns:reqif = http://www.omg.org/spec/ReqIF/20110401/reqif.xsd >
< STRING > 某些str < / STRING >
< PART >
< AA xmlns = http://www.w3.org/1999/xhtml > 部分字符串< / AA >
< BB > 99 < / BB >
< / PART >
< / Container >
我对xml和c#都很陌生,因此不太清楚很多事情。请帮我。 Thanx寻求帮助。
我的第一个问题是我要查找的输出有两个不同的前缀指向同一个命名空间,即xmlns:xhtml以及xmlns:reqif-xhtml指的是到同一个uri
这个问题对我来说并不清楚。为什么不使用比数据合同更好的序列化?请参阅:
http://msdn.microsoft.com/en-us /library/ms733127.aspx [ ^ ]。
在这种情况下,您不需要直接使用XML和任何XML属性;而且,这种方法是最具侵入性的。请看我过去的答案:
如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],
创建属性文件...... [ ^ ]。
-SA
I am trying to serialize my following code :
[Serializable]
public class Part
{
[XmlElementAttribute("AA")]
public string a;
[XmlElementAttribute("BB")]
public int b;
public Part()
{
a = "part string"; b = 99;
}
}
[Serializable]
public class Container
{
[XmlElementAttribute("STRING")]
public string str;
[XmlElementAttribute("PART")]
public Part part;
public Container()
{
str = "some str"; part = new Part();
}
}
class Program
{
static void Main(string[] args)
{
Container container = new Container();
TextWriter write = new StreamWriter("MyNewClass.xml");
XmlWriterSettings settings = new XmlWriterSettings();
settings.IndentChars = "\t";
settings.Indent = true;
XmlWriter xmlWriter = XmlWriter.Create(write, settings);
XmlSerializer ser = new XmlSerializer(container.GetType());
ser.Serialize(xmlWriter, container);
xmlWriter.Close(); write.Close();
}
}
The output that I am getting is as follows :
<?xml version="1.0" encoding="utf-8"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<STRING>some str</STRING>
<PART>
<AA>part string</AA>
<BB>99</BB>
</PART>
</Container>
I have tried to add namespace etc information in a no of ways. But I have not succeeded. I want an output that looks as follows after serialization :
<?xml version="1.0" encoding="utf-8"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd reqif.xsd http://www.omg.org/spec/ReqIF/20110401/reqif-xhtml reqif-xhtml.xsd" xmlns:reqif-xhtml="http://www.w3.org/1999/xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:doors="http://www.ibm.com/rdm/doors/REQIF/xmlns/1.0" xmlns:rm-reqif="http://www.ibm.com/rm/reqif" xmlns:rm="http://www.ibm.com/rm" xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:reqif="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd">
<STRING>some str</STRING>
<PART>
<AA xmlns="http://www.w3.org/1999/xhtml">part string</AA>
<BB>99</BB>
</PART>
</Container>
I am quite new to xml and c# and thus not very clear about lot of things. Please help me. Thanx for help.
My first problem is that the output that I am looking for has two different prefixes pointing to the same namespace, i.e, both xmlns:xhtml as well as xmlns:reqif-xhtml are referring to the same uri
The problem is not clear to me. Why not using such a much better serialization as Data Contract? Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
In this case, you won't need to work with XML and any XML properties directly; also, this approach is the most non-intrusive. Please see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].
—SA
这篇关于我无法正确添加命名空间信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!