如何用C#构建xml看起来像这样 [英] How to build xml with C# look like this

查看:78
本文介绍了如何用C#构建xml看起来像这样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有数据集源代码视图,使用c#创建xml文件并将数据发送到数据集



 <?  xml     version   =  1.0   编码  =  utf-8    >  

< 公司 >

< 员工 >

< 员工 名称 = Mahesh Chand < span class =code-attribute>年龄 = 30 电话 = 6101233333 / >

< 员工 名称 = Rose Garner 年龄 = 56 电话 = 2133428778 < span class =code-keyword> / >

< 员工 名称 = Amger Jap 年龄 = 22 电话 = 9092349800 / >

< 员工 < span class =code-attribute>名称 = Mike Gold 年龄 = 35 电话 = 9908088823 / >

< 员工 名称 = Renee Flower 年龄 = 19 < span class =code-attribute>电话 = 4848901003 / >

< span class =code-keyword>< / Employees >

< /公司 >



我试过这段代码但它不起作用(xml不像最顶层的那样)

 XmlDocument doc =  new  XmlDocument(); 

// (1)建议使用xml声明,但不是强制性的
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( 1.0 UTF-8 null );
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration,root);

// (2)string.Empty使代码更清晰
XmlElement element1 = doc.CreateElement( string .Empty, body string .Empty);
doc.AppendChild(element1);

XmlElement element2 = doc.CreateElement( string .Empty, level1 string .Empty);
element1.AppendChild(element2);

XmlElement element3 = doc.CreateElement( string .Empty, level2 string .Empty);
XmlText text1 = doc.CreateTextNode( text = + );
element3.AppendChild(text1);
element2.AppendChild(element3);

XmlElement element4 = doc.CreateElement( string .Empty, level2 string .Empty);
XmlText text2 = doc.CreateTextNode( 其他文字);
element4.AppendChild(text2);
element2.AppendChild(element4);

doc.Save( ChartReport.xml);

DataSet ds = new DataSet();
ds.ReadXml( ChartReport.xml);
ds.WriteXmlSchema( DataSet1.xsd);

this .reportViewer1.RefreshReport();

解决方案

1)XML只是文本,因此您可以使用StringBuilder <构建它href =http://www.devx.com/tips/Tip/41390target =_ blanktitle =New Window> ^ ]如果你喜欢



2)您可以按照您的尝试构建XML,但当然,您需要调整代码。像一个完整的复制粘贴将无济于事。请参阅 LinqPad [ ^ ]示例:

  private  XmlNode NewEmployee(XmlDocument doc,字符串名称, int 年龄,字符串电话)
{
var e = doc.CreateElement( 雇员);
var aA = doc.CreateAttribute( 年龄);
aA.Value = age.ToString();

var aN = doc.CreateAttribute( 名称);
aN.Value = name;

var aP = doc.CreateAttribute( 电话);
aP.Value =电话;

e.Attributes.Append(aN);
e.Attributes.Append(aA);
e.Attributes.Append(aP);

return e;
}

void Main()
{
var doc = new XmlDocument();
var root = doc.CreateElement( 公司);
doc.AppendChild(root);
var es = doc.CreateElement( 雇员);
root.AppendChild(es);

es.AppendChild(NewEmployee(doc, Mahesh Chand,< span class =code-digit> 30
6101233333));
es.AppendChild(NewEmployee(doc, Rose Garner 56 2133428778));

doc.DumpFormatted();
}



3)而且你可以使用序列化和强类型类。请参阅以下示例:

 [XmlRoot(  Company )] 
public class 公司
{
[XmlArrayItem( 员工 typeof (员工))]
[XmlArray( 员工)]
< span class =code-keyword> public 列表<员工>雇员;
}

public class 员工
{
[XmlAttribute( 名称)]
public string 名称{ get ; set ;}
[XmlAttribute( 年龄)]
public int 年龄{获得; set ;}
[XmlAttribute( Phone )]
public string 电话{获取; 设置;}
}

void Main()
{
var c = new 公司( );
c.Employees = new 列表<员工>()
{
new Employee(){Name = Mahesh Chand,Age = 30 ,Phone = 6101233333},
< span class =code-keyword> new
Employee(){Name = Rose Garner,Age = 56 ,Phone = 2133428778 }
};


XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( string .Empty, string .Empty);
TextWriter w = new StringWriter();
var s = new XmlSerializer( typeof < /跨度>(公司));
s.Serialize(w,c,ns);
w.Flush();


w.ToString()。Dump();
}


Google是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...



使用部分问题进行快速搜索,因为搜索字词超过2,000,000次点击:< a href =https://www.google.co.uk/search?q=create+xml+file+using+c%23&oq=create+xml+file+using+c%23&aqs=chrome。 .69i57& sourceid = chrome& es_sm = 0& ie = UTF-8> Google使用c#创建xml文件 [ ^ ]

第二个命中是MSDN指导您完成整个过程:MSDN:创建XML文件 [ ^ ]



将来,请尝试自己做至少基础研究,而不是浪费你的时间还是我们的。

目前还不清楚你真正想做什么,因为你的代码远远超出了呈现的XML数据。



如果要创建具有与您所呈现的结构相同的XML文件,则需要添加几个 XmlAttribute [ ^ ]到您的XmlElement。



在下面的代码中,我创建了第一个元素。添加更多只是重复的问题。

 XmlDocument doc =  new  XmlDocument(); 

// (1)建议使用xml声明,但不是强制性的
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( 1.0 UTF-8 null );
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration,root);

XmlElement elementRoot = doc.CreateElement( string .Empty, 公司 string .Empty);
doc.AppendChild(elementRoot);

// (2)string.Empty使代码更清晰
XmlElement element1 = doc.CreateElement( string .Empty, 员工 string .Empty);
elementRoot.AppendChild(element1);

XmlElement element2 = doc.CreateElement( string .Empty, Employee string .Empty);

// 创建和添加名称属性
XmlAttribute attrib1 = doc.CreateAttribute( Name);
attrib1.Value = Mahesh Chand;
element2.Attributes.Append(attrib1);

// 创建并添加Age属性
XmlAttribute attrib2 = doc.CreateAttribute( 年龄);
attrib2.Value = 30;
element2.Attributes.Append(attrib2);

// 创建和添加电话属性
XmlAttribute attrib3 = doc.CreateAttribute( Phone);
attrib3.Value = 6101233333;
element2.Attributes.Append(attrib3);

element1.AppendChild(element2);

// 添加更多带属性的元素
// ...

doc.Save( ChartReport.xml);





说完了,问题出现了为什么你要先创建一个XML文件,然后将其加载到数据集中。


And also the code view in Dataset Source, create xml file using c# and send the data to dataset

<?xml version="1.0" encoding="utf-8" ?> 

<Company>

<Employees>

  <Employee Name="Mahesh Chand" Age="30" Phone="6101233333" /> 

  <Employee Name="Rose Garner" Age="56" Phone="2133428778" /> 

  <Employee Name="Amger Jap" Age="22" Phone="9092349800" /> 

  <Employee Name="Mike Gold" Age="35" Phone="9908088823" /> 

  <Employee Name="Renee Flower" Age="19" Phone="4848901003" /> 

</Employees>

</Company>


I have tried this code but its not working ( the xml not like the top one)

XmlDocument doc = new XmlDocument();

//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);

//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement(string.Empty, "body", string.Empty);
doc.AppendChild(element1);

XmlElement element2 = doc.CreateElement(string.Empty, "level1", string.Empty);
element1.AppendChild(element2);

XmlElement element3 = doc.CreateElement(string.Empty, "level2", string.Empty);
XmlText text1 = doc.CreateTextNode("text = "+"");
element3.AppendChild(text1);
element2.AppendChild(element3);

XmlElement element4 = doc.CreateElement(string.Empty, "level2", string.Empty);
XmlText text2 = doc.CreateTextNode("other text");
element4.AppendChild(text2);
element2.AppendChild(element4);

doc.Save("ChartReport.xml");

DataSet ds = new DataSet();
ds.ReadXml("ChartReport.xml");
ds.WriteXmlSchema("DataSet1.xsd");

this.reportViewer1.RefreshReport();

解决方案

1) XML is just text, so you have the possibility to build it with StringBuilder[^] if you like

2) You can build XML as you tried, but of course, you need to adapt the code. Copy-pasting like a full won't help. See LinqPad[^] sample:

private XmlNode NewEmployee(XmlDocument doc, string name, int age, string phone)
{
  var e = doc.CreateElement("Employee");
  var aA = doc.CreateAttribute("Age");
  aA.Value = age.ToString();
  
  var aN = doc.CreateAttribute("Name");
  aN.Value = name;
  
  var aP = doc.CreateAttribute("Phone");
  aP.Value = phone;
  
  e.Attributes.Append(aN);
  e.Attributes.Append(aA);
  e.Attributes.Append(aP);
  
  return e;
}

void Main()
{
	var doc = new XmlDocument();
	var root = doc.CreateElement("Company");
	doc.AppendChild(root);
	var es = doc.CreateElement("Employees");
	root.AppendChild(es);
	
	es.AppendChild(NewEmployee(doc, "Mahesh Chand", 30, "6101233333"));
	es.AppendChild(NewEmployee(doc, "Rose Garner",56, "2133428778"));
	
	doc.DumpFormatted();
}


3) And of couse you can use serialization, and strongly typed classes. See following example:

[XmlRoot("Company")]
public class Company
{
	[XmlArrayItem("Employee", typeof(Employee))]
    [XmlArray("Employees")]
	public List<Employee> Employees;
}

public class Employee
{
 [XmlAttribute("Name")]
 public string Name {get; set;}
 [XmlAttribute("Age")]
 public int Age {get; set;}
 [XmlAttribute("Phone")]
 public string Phone {get;set;}
}

void Main()
{
	var c = new Company();
	c.Employees = new List<Employee>() 
		{
			new Employee() {Name = "Mahesh Chand", Age=30, Phone = "6101233333"},
			new Employee() {Name = "Rose Garner", Age=56, Phone="2133428778"}
		};
	
	
	XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
	ns.Add(string.Empty, string.Empty);
	TextWriter w = new StringWriter();
    var s = new XmlSerializer(typeof(Company));
    s.Serialize(w, c, ns);
    w.Flush();
	
	
	w.ToString().Dump();
}


Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using part of your question as the search term gave over 2,000,000 hits: Google "Create xml file using c#"[^]
The second hit is MSDN guiding you through the process: MSDN: "Creating an XML File"[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.


It is not exactly clear what it is you actually want to do, because your code is faraway from the presented XML data.

If you want to create an XML file with the same structure as you have presented, you need to add several XmlAttribute[^] to your XmlElement.

In the code below I have created the first element. Adding more is just a matter of repetition.

XmlDocument doc = new XmlDocument();

//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);

XmlElement elementRoot = doc.CreateElement(string.Empty, "Company", string.Empty);
doc.AppendChild(elementRoot);

//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement(string.Empty, "Employees", string.Empty);
elementRoot.AppendChild(element1);

XmlElement element2 = doc.CreateElement(string.Empty, "Employee", string.Empty);

// Create and add Name attribute
XmlAttribute attrib1 = doc.CreateAttribute("Name");
attrib1.Value = "Mahesh Chand";
element2.Attributes.Append(attrib1);

// Create and add Age attribute
XmlAttribute attrib2 = doc.CreateAttribute("Age");
attrib2.Value = "30";
element2.Attributes.Append(attrib2);

// Create and add Phone attribute
XmlAttribute attrib3 = doc.CreateAttribute("Phone");
attrib3.Value = "6101233333";
element2.Attributes.Append(attrib3);

element1.AppendChild(element2);

// Add more elements with attributes
// ...

doc.Save("ChartReport.xml");



That said and done, the question arise why you want to first create an XML file and then load it into the dataset.


这篇关于如何用C#构建xml看起来像这样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆