我有一个XDocument对象,ToString()方法返回XML而没有任何缩进.如何从包含缩进的XML创建字符串?
编辑:我问的是如何创建一个内存字符串而不是写出文件.
编辑:看起来我不小心在这里问了一个技巧问题... ToString()确实返回缩进的XML.
我看过Nodes()vs DescendantNodes()用法?看看和之间的区别.Nodes(),.DescendantNodes()但有什么区别:
XDocument.Descendants()和XDocument.DescendantNodes()?
var xmlDoc = XDocument.Load(@"c:\Projects\Fun\LINQ\LINQ\App.config");
var descendants = xmlDoc.Descendants();
var descendantNodes = xmlDoc.DescendantNodes();
foreach (var d in descendants)
Console.WriteLine(d);
foreach (var d in descendantNodes)
Console.WriteLine(d);
Run Code Online (Sandbox Code Playgroud) 这可能是一个初学者xml问题,但是如何生成如下所示的xml文档呢?
<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
<ci:field1>test</ci:field1>
<ca:field2>another test</ca:field2>
</root>
Run Code Online (Sandbox Code Playgroud)
如果我可以写这个,我可以解决我的其余问题.
理想情况下,我想使用c#使用LINQ to XML(XElement,XNamespace等),但如果使用XmlDocuments和XmlElements可以更容易/更好地实现这一点,我会继续使用它.
谢谢!!!
有关XDocuments和Linq的新手,请建议一个从xml字符串中的特定标记中检索数据的解决方案:
如果我有一个来自webservice响应的XML字符串(为了方便我格式化了xml):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCashFlowReportResponse xmlns="http://tempuri.org/">
<GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf>
</GetCashFlowReportResponse>
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
使用以下代码,仅当GetCashFlowReportResponsetag没有"xmlns"属性时才能获取值.不知道为什么?否则,它始终返回null.
string inputString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><GetCashFlowReportResponse xmlns=\"http://tempuri.org/\"><GetCashFlowReportPdf>Hello!</GetCashFlowReportPdf></GetCashFlowReportResponse></soap:Body></soap:Envelope>"
XDocument xDoc = XDocument.Parse(inputString);
//XNamespace ns = "http://tempuri.org/";
XNamespace ns = XNamespace.Get("http://tempuri.org/");
var data = from c in xDoc.Descendants(ns + "GetCashFlowReportResponse")
select (string)c.Element("GetCashFlowReportPdf");
foreach (string val in data)
{
Console.WriteLine(val);
}
Run Code Online (Sandbox Code Playgroud)
我无法更改Web服务以删除该属性.是否有更好的方法来读取响应并将实际数据返回给用户(以更易读的形式)?
编辑: 解决方案:
XDocument xDoc = XDocument.Parse(inputString);
XNamespace ns = "http://tempuri.org/";
var data = from c in xDoc.Descendants(ns …Run Code Online (Sandbox Code Playgroud) 如何将XElement转换为XDocument?有没有内置的方法呢?我能想到的唯一方法就是没有new XDocument(xelement.ToString())它会导致创建大字符串然后解析它们,从而降低性能.
我有两个问题:
1.我已经开始使用Linq解决XML问题了,我想知道是否可以通过Linq更改XML文档.我的意思是,有什么喜欢的
XDocument xmlDoc = XDocument.Load("sample.xml");
update item in xmlDoc.Descendants("item")
where (int)item .Attribute("id") == id
...
Run Code Online (Sandbox Code Playgroud)
2.我已经知道如何通过简单地使用创建和添加新的XMLElement
xmlDoc.Element("items").Add(new XElement(......);
Run Code Online (Sandbox Code Playgroud)
但是如何删除单个条目?
XML样本数据:
<items>
<item id="1" name="sample1" info="sample1 info" web="" />
<item id="2" name="sample2" info="sample2 info" web="" />
</itmes>
Run Code Online (Sandbox Code Playgroud) // Remove element with ID of 1
var userIds = from user in document.Descendants("Id")
where user.Value == "1"
select user;
userIds.Remove();
SaveAndDisplay(document);
// Add element back
var newElement = new XElement("Id", "0",
new XElement("Balance", "3000"));
document.Add(newElement);
SaveAndDisplay(document);
Run Code Online (Sandbox Code Playgroud)
add元素后块是个问题.当它到达添加时它表明:
此操作将创建结构不正确的文档.
我犯的是什么愚蠢的错误?
编辑:
是的,我是在读XDocument,而不是XElement.关于何时支持其中一个的任何建议?
我正在从表创建一个新的XDocument.我必须从XSD文档验证文档并且它一直失败,因为它在不应该的时候将xmlns =""添加到其中一个元素.以下是相关代码的一部分.
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xmlns = "https://uidataexchange.org/schemas";
XElement EmployerTPASeparationResponse = null;
XElement EmployerTPASeparationResponseCollection = new XElement(xmlns + "EmployerTPASeparationResponseCollection", new XAttribute(XNamespace.Xmlns + "xsi", xsi), new XAttribute(xsi + "schemaLocation", "https://uidataexchange.org/schemas SeparationResponse.xsd"));
XDocument doc = new XDocument(
new XDeclaration("1.0", null, "yes"), EmployerTPASeparationResponseCollection);
//sample XElement populate Element from database
StateRequestRecordGUID = new XElement("StateRequestRecordGUID");
StateRequestRecordGUID.SetValue(rdr["StateRequestRecordGUID"].ToString());
//sample to add Elements to EmployerTPASeparationResponse
EmployerTPASeparationResponse = new XElement("EmployerTPASeparationResponse");
if (StateRequestRecordGUID != null)
{
EmployerTPASeparationResponse.Add(StateRequestRecordGUID);
}
//the part where I add the EmployerTPASeparationResponse collection to the …Run Code Online (Sandbox Code Playgroud) 在我看来web method,我得到了一些第三方C#实体类的对象.实体类只不过是DataContract.这个实体类非常复杂,具有各种类型的属性,一些属性也是集合.当然,这些链接类型也是DataContracts.
我想将该DataContract实体序列化为XML,作为我的Web服务的业务逻辑的一部分.我不能DataContractSerializer直接使用(在我在web方法中收到的对象),因为XML模式完全不同. 因此,DataContractSerializer生成的XML不会针对模式进行验证.
我无法总结出我应该遵循的方法来实施.我可以想到以下实施方法:
LINQ to XML - 这看起来不错,但我需要为每种类型的对象手动创建XML树(即类实例的元素或XML表示).由于有许多实体类并且它们彼此链接,我认为手动编写XML元素太多了.此外,当实体类引入一些新属性时,我将不得不继续修改XML树.不仅如此,我生成XML树的代码看起来有点笨拙(至少在外观上)并且将来很难被其他开发人员维护/更改; 他/她将不得不仔细研究它以了解如何生成XML.
XmlSerializer - 我可以编写自己的实体类来表示我想要的XML结构.现在,我需要将传入对象的详细信息复制到我自己的类的对象中.所以这是额外的工作(对于.NET,当代码执行时也是如此!).然后我可以XmlSerializer在我的对象上使用生成XML.在这种情况下,我将不得不创建实体类,每当第三方实体被修改时,我只需要在我的类中添加新属性.(使用XmlElement或XmlAttibute属性).但人们推荐DataContractSerializer这个,所以我不想最终确定这个,除非我清楚所有方面.
DataContractSerializer - 再次在这里,我将不得不编写自己的实体类,因为我无法控制第三方DataContracts.我需要将传入对象的细节复制到我自己的类的对象中.所以这是额外的工作.但是,由于DataContractSerializer不支持Xml属性,因此我必须IXmlSerializable在WriteXml方法中实现并生成所需的Xml .DataContractSerializer比XmlSerializer更快,但如果第三方实体发生变化,我将不得不处理更改(在WriteXml中).
问题:
DataContractSerializer值得考虑(因为它具有更好的性能XmlSerilaizer)?serialization xml-serialization .net-4.0 linq-to-xml datacontractserializer
我是新手,XML并尝试了以下但我得到了一个例外.有人能帮我吗?
例外是 This operation would create an incorrectly structured document
我的代码:
string strPath = Server.MapPath("sample.xml");
XDocument doc;
if (!System.IO.File.Exists(strPath))
{
doc = new XDocument(
new XElement("Employees",
new XElement("Employee",
new XAttribute("id", 1),
new XElement("EmpName", "XYZ"))),
new XElement("Departments",
new XElement("Department",
new XAttribute("id", 1),
new XElement("DeptName", "CS"))));
doc.Save(strPath);
}
Run Code Online (Sandbox Code Playgroud)